package com.eipc.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.androidpn.sdk.Constants;
import org.androidpn.sdk.XmppManager;
import org.json.JSONArray;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.util.Log;
import com.eipc.CommonActivity;
import com.eipc.MainActivity;
import com.eipc.model.Consult;
import com.eipc.model.Department;
import com.eipc.model.Game;
import com.eipc.model.ImportNotice;
import com.eipc.model.Match;
import com.eipc.model.News;
import com.eipc.model.Person;
import com.eipc.model.Player;
import com.eipc.model.PlayerGroup;
import com.eipc.model.Position;
import com.eipc.model.Reply;
import com.eipc.model.Score;
import com.eipc.model.TEverydayAround;
import com.eipc.model.TEverydayAround.TEverydayGroup;
import com.eipc.model.TEverydayAround.TEverydayMatch;
import com.eipc.model.TPlayer;
import com.eipc.model.TSignTable;
import com.eipc.model.TSignTable.TSignTableGroup;
import com.eipc.model.TSignTable.TSignTableMatch;
import com.eipc.model.Weather;

/**
 * 
 * @author x_liaolijun
 *
 * 2010-9-7 下午02:23:28
 *  
 * MidDbHelper
 *
 */
public class DBHelper extends android.database.sqlite.SQLiteOpenHelper {
	
	public final static int APP_TYPE_IMPORT_NOTICE = 1;   //重要通知
	public final static int APP_TYPE_ME_REPORT = 2;       //我要报告
	public final static int APP_TYPE_WORK_REPORT = 3;     //工作汇报
	
	private SharedPreferences sdkPreferences;

	PreferencesHelper mHelper;
	SQLiteDatabase db;
	Context context;

	public DBHelper(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
		mHelper = new PreferencesHelper(context.getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE));
		try{
			SharedPreferences sdkPreferences = context.getSharedPreferences(Constants.SDK_PREFERENCES,Context.MODE_PRIVATE);
			String uploadReplyStatus = sdkPreferences.getString("UPLOAD_REPLY_STATUS","1");
			Log.e("-------uploadReplyStatus-----------",uploadReplyStatus);
			if("1".equals(uploadReplyStatus)){
				openDB();
				db.execSQL(TB_DELETE + TB_UPLOAD_REPLY);
				db.execSQL(SQL_UPLOAD_REPLY);
				initUploadReply();
				Editor editor = sdkPreferences.edit();
				editor.putString("UPLOAD_REPLY_STATUS","2");
		        editor.commit();
			}
		}catch(Exception e){
			Log.e("----uploadReplysql-----","-----error-----",e);
		}
	}
	
	@Override
	public void onCreate(SQLiteDatabase db){
		try{
			db.execSQL(SQL_POSITION);
			db.execSQL(SQL_DEPARTMENT);
			db.execSQL(SQL_IMPORT_NOTICE);
			db.execSQL(SQL_IMPORT_NOTICE_SEND);
			db.execSQL(SQL_IMPORT_NOTICE_SEND_CHILD);
			db.execSQL(SQL_PERSON);
			db.execSQL(SQL_PERSON2);
			db.execSQL(SQL_PLAYER);
			db.execSQL(SQL_MATCH);
			db.execSQL(SQL_PLAYER_GROUP);
			db.execSQL(SQL_TURN);
			db.execSQL(SQL_DATASTATISTICS);
			db.execSQL(SQL_GAMES);
			db.execSQL(SQL_TOTAL);
			db.execSQL(SQL_SENTENCE_SCORE);
			db.execSQL(SQL_VENUE);
			db.execSQL(SQL_SCORE);
			db.execSQL(SQL_WEAHTER);
			db.execSQL(SQL_CONSULT);
			db.execSQL(SQL_TB_LATEST_SCORE);
			
			insertFromAsset(db, new String[]{"person.txt",
					"person2.txt",
					"postion.txt",
					"department.txt",
					"ground1.txt",
					"person2.txt",});
			
			if(!mHelper.getBoolean("is_insert")){
				new ContactsTask().execute();
				mHelper.put("is_insert",true);
			}
		}catch(Exception e){
			Log.e("DBHelper onCreate","error");
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		try{
			db.execSQL(TB_DELETE + TB_POSITION);
			db.execSQL(TB_DELETE + TB_DEPARTMENT);
			db.execSQL(TB_DELETE + TB_IMPORT_NOTICE);
			db.execSQL(TB_DELETE + TB_IMPORT_NOTICE_SEND);
			db.execSQL(TB_DELETE + TB_IMPORT_NOTICE_SEND_CHILD);
			db.execSQL(TB_DELETE + TB_PERSON);
			db.execSQL(TB_DELETE + TB_PLAYER);
			db.execSQL(TB_DELETE + TB_MATCH);
			db.execSQL(TB_DELETE + TB_PLAYER_GROUP);
			db.execSQL(TB_DELETE + TB_DATASTATICSTICS);
			db.execSQL(TB_DELETE + TB_GAMES);
			db.execSQL(TB_DELETE + TB_TOTAL);
			db.execSQL(TB_DELETE + TB_TURN);
			db.execSQL(TB_DELETE + TB_SENTENCE_SCORE);
			db.execSQL(TB_DELETE + TB_SCORE);
			db.execSQL(TB_DELETE + TB_VENUES);
			db.execSQL(TB_DELETE + TB_WEATHER);
			db.execSQL(TB_DELETE + TB_CONSULT);
			
			onCreate(db);
		}catch(Exception e){
			Log.e("---------onUpgrade","Error");
		}
	}

	/*
	 * 重文件中读取数据
	 */
	private void insertFromAsset(SQLiteDatabase db,String[] files){
		AssetManager manager = context.getAssets();
		DataInputStream dis = null;
		 BufferedReader bf = null;
		String temp = "";
		for(int i=0;i<files.length;i++){
			try {
				dis = new DataInputStream(manager.open(files[i]));
				bf = new BufferedReader(new InputStreamReader(dis)); 
				while((temp=bf.readLine())!=null){
					if(temp!=null && !"".equals(temp)) db.execSQL(temp);
				}
			} catch (Exception e) {
				Log.w("", e);
			}
			finally{
				try {
					if(bf!=null) bf.close();
					if(dis!=null) dis.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	void openDB() {
		try {
			if(db==null) db = getWritableDatabase();
		} catch (SQLiteException ex) {
			db = getReadableDatabase();
		}
	}
	
	public String getMaxId(String table,String field){
		openDB();
		String result = null;
		Cursor c = db.query(table, new String[]{"max("+field+")"}, null, null, null, null,null);
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			String r = c.getString(0);
			c.close();
			return r;
		}
		return result;
	}
	
	public int[] getAllTurn(String isGroup){
		openDB();
		String sql = "select distinct "+FIELD_MATCH_TURN_ID+" from "+TB_MATCH+" where "+FIELD_MATCH_IS_GROUP+"= '"+isGroup+"'";
		Cursor c = null;
		try{
			c = db.rawQuery(sql, null);
			int[] values = null;
			if(c!=null && c.getCount()>0){
				int len = c.getCount();
				values = new int[len];
				c.moveToFirst();
				for(int i=0;i<len;i++){
					values[i] = c.getInt(0);
					c.moveToNext();
				}
			}
			return values;
		}
		catch(Exception e){
			Log.w("",e);
			return null;
		}
		finally{
			if(c!=null) c.close();
		}
	}
	
	public int[] getAllMatchDay(){
		openDB();
		String sql = "select distinct "+FIELD_MATCH_DAY+" from "+TB_MATCH;
		Cursor c = null;
		try{
			c = db.rawQuery(sql, null);
			int[] values = null;
			if(c!=null && c.getCount()>0){
				int len = c.getCount();
				values = new int[len];
				c.moveToFirst();
				for(int i=0;i<len;i++){
					values[i] = c.getInt(0);
					c.moveToNext();
				}
			}
			return values;
		}
		catch(Exception e){
			Log.w("", e);
			return null;
		}
		finally{
			if(c!=null) c.close();
		}
	}
	
	public void updateMaxId(String maxID){
		SharedPreferences sdkPreferences = context.getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
		Editor editor = sdkPreferences.edit();
		editor.putString(Constants.MaxId,String.valueOf(maxID));
        editor.commit();
	}
	
	public String getConsultIDs(){
		String strResult = "";
		openDB();	
		Cursor c = queryAll(TB_CONSULT);
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			do{
				String r = c.getString(c.getColumnIndexOrThrow(FIELD_CONSULT_ID));
				if(c.isLast()){
					strResult += r;
				}else{
					strResult += r+",";
				}
			}
			while(c.moveToNext());
		}
		if(c!=null)c.close();
		return strResult;
		
	}
	
	public String getIDs(){
		String strResult = "";
		openDB();		
		sdkPreferences = context.getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
		int iMaxId = Integer.valueOf(sdkPreferences.getString(Constants.MaxId,"0"));
		String select = FIELD_IMPORT_NOTICE_ID+" > "+ String.valueOf(iMaxId);
		Cursor c = db.query(TB_IMPORT_NOTICE,new String[]{FIELD_IMPORT_NOTICE_ID},select,null,null,null,null);
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			do{
				String r = c.getString(c.getColumnIndexOrThrow(FIELD_IMPORT_NOTICE_ID));
				if (c.isLast()){
					strResult += r;
				}else{
					strResult += r+",";
				}
			}
			while(c.moveToNext());
		}
		if(c!=null) c.close();
		return strResult;
		
	}
	
	/**
	 * 求最新的分数
	 */
	public Game getNewScore(int language){
		openDB();
		Cursor c = null;
		Cursor c1 = null;
		Cursor c2 = null;
		Game game = null;
		try {
			String sql = "select * from "+TB_SCORE+" where "+FIELD_SCORE_MATCH_ID+"=(select "+FIELD_SCORE_MATCH_ID+" from "+TB_SCORE+" where "+FIELD_SCORE_NUM+"=(select max("+FIELD_SCORE_NUM+") from "+TB_SCORE+")) order by "+FIELD_SCORE_ID;
			c = db.rawQuery(sql, null);
			String matchId = "";
			if(c!=null && c.getCount()>0){
				game = new Game(); 
				c.moveToFirst();
				int len = c.getCount();
				matchId = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_MATCH_ID));
				for(int i=0;i<len;i++){
					if(i!=0){
						game.oneScore+="|";
						game.otherScore+="|";
					}
					game.oneScore+=c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_A));
					game.otherScore+=c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_B));
					c.moveToNext();
				}
				c = queryAll(TB_MATCH, new String[]{FIELD_MATCH_ID},new String[]{matchId});
				if(c!=null&&c.getCount()>0){
					c.moveToFirst();
					if(STATUS.MATCH_IS_GROUP_NO.equals(c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_IS_GROUP)))){
						c1 = queryAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_A))});
						c1.moveToFirst();
						if(language==CommonActivity.LANGUAGE_CHINESE)
							game.one = c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_CN_SNAME));
						else
							game.one = c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_EN_SNAME));
						c1 = queryAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_B))});
						c1.moveToFirst();
						if(language==CommonActivity.LANGUAGE_CHINESE)
							game.other = c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_CN_SNAME));
						else
							game.other = c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_EN_SNAME));
					}else{
						String name = "";
						c1 = queryAll(TB_PLAYER_GROUP, new String[]{FIELD_PLAYER_GAME_ID},new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_A))});
						c1.moveToFirst();
						c2 = queryOrAll(TB_PLAYER,new String[]{FIELD_PLAYER_ID,FIELD_PLAYER_ID},new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_A)),c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_B))});
						c2.moveToFirst();
						do{
							if(!"".equals(name)) name += " ";
							if(language==CommonActivity.LANGUAGE_CHINESE)
								name = c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_CN_SNAME));
							else
								name = c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_EN_SNAME));
						}
						while(c2.moveToNext());
						c1 = queryAll(TB_PLAYER_GROUP, new String[]{FIELD_PLAYER_GAME_ID},new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_A))});
						c1.moveToFirst();
						c2 = queryOrAll(TB_PLAYER,new String[]{FIELD_PLAYER_ID,FIELD_PLAYER_ID},new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_A)),c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_B))});
						c2.moveToFirst();
						do{
							if(!"".equals(name)) name += " ";
							if(language==CommonActivity.LANGUAGE_CHINESE)
								name = c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_CN_SNAME));
							else
								name = c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_EN_SNAME));
						}
						while(c2.moveToNext());
					}
				}
				return game;
			}
		} catch (Exception e) {
			Log.w("", e);
			return null;
		}finally{
			if(c!=null) c.close();
			if(c1!=null) c1.close();
			if(c2!=null) c1.close();
		}
		return null;
	}
	
	public ArrayList<News> getTenNews(){
		ArrayList<News> news = new ArrayList<News>(10);
		openDB();
		int i = 0;
		News n = null;
		Cursor c = db.query(TB_IMPORT_NOTICE, null,FIELD_IMPORT_NOTICE_ISSUE_TIME+" like ? ",new String[]{"%"+Tools.getCurrentDateString()+"%"}, null, null, FIELD_IMPORT_NOTICE_TOP+" desc,"+FIELD_IMPORT_NOTICE_ISREAD);
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			do{
				if(i<10){
					n = new News(c.getString(c.getColumnIndex(FIELD_IMPORT_NOTICE_SEND_TYPE)),
								c.getString(c.getColumnIndexOrThrow(FIELD_IMPORT_NOTICE_CONTENT)), 
								c.getInt(c.getColumnIndexOrThrow(FIELD_IMPORT_NOTICE_APPTYPE)),
								c.getInt(c.getColumnIndexOrThrow(FIELD_ID)),
								c.getString(c.getColumnIndex(FIELD_IMPORT_NOTICE_SEND_TIME)));
					i++;
					news.add(n);
				}
			}
			while(c.moveToNext());
		}
		if(c!=null) c.close();
		return news;
	}
	
	public void insertImportNotice(ImportNotice[] infos,String maxID){
		Log.e("-----insert-------",infos.toString());
		if(infos!=null){
			openDB();
			ContentValues values;
			for(ImportNotice info : infos){
				Log.e("-----insert-noticeId-------",String.valueOf(info.noticeId));
				try{
					Cursor c = queryAll(TB_IMPORT_NOTICE,new String[]{FIELD_IMPORT_NOTICE_ID}, new String[]{String.valueOf(info.noticeId)});
					if(c==null||c.getCount()<1){
						Log.e("-----insert-begin-------",info.content);
						values = new ContentValues();
						values.put(FIELD_IMPORT_NOTICE_ID, info.noticeId);
						values.put(FIELD_IMPORT_NOTICE_IMAGE_LOCALURL, info.imageLocalUrl);
						values.put(FIELD_IMPORT_NOTICE_IMAGE_WEBURL, info.imageWebUrl);
						values.put(FIELD_IMPORT_NOTICE_SEND_TYPE, info.sendType);
						values.put(FIELD_IMPORT_NOTICE_APPTYPE, info.appType);
						values.put(FIELD_IMPORT_NOTICE_CONTENT, info.content);
						values.put(FIELD_IMPORT_NOTICE_ISSUER, info.issuer);
						values.put(FIELD_IMPORT_NOTICE_ISSUE_TIME, info.issueTime);
						values.put(FIELD_IMPORT_NOTICE_SEND_TIME, info.sendTime);
						values.put(FIELD_IMPORT_NOTICE_SENDER, info.sender);
						values.put(FIELD_IMPORT_NOTICE_ISREAD, info.isRead);
						values.put(FIELD_IMPORT_NOTICE_TOP, info.top);
						values.put(FIELD_IMPORT_NOTICE_REPLYCONTENT, info.replyContent);
						values.put(FIELD_IMPORT_NOTICE_REPLYTIME, info.replyTime);
						db.insert(TB_IMPORT_NOTICE, FIELD_ID, values);
					}else{
						Log.e("-----insert-noticeId-exsist-------",String.valueOf(info.noticeId));
					}
				}catch(Exception e){
					Log.e("insert error : ",e.getMessage());
				}
				XmppManager.uploadNotification(context,true);
				Intent i = new Intent(MainActivity.ACTION_POPUP_MESSAGE);
	    		i.putExtra("id",maxID);
	    		context.sendBroadcast(i);
			}
		}
	}
	
	public void insertImportNotice(ArrayList<ImportNotice> infos){
		Log.e("-----insert-------",infos.toString());
		if(infos!=null){
			openDB();
			ContentValues values;
			///String maxId = getMaxId(TB_IMPORT_NOTICE,FIELD_IMPORT_NOTICE_ID);
			//Log.e("-----insert-maxId-------",maxId);
			for(ImportNotice info : infos){
				Log.e("-----insert-noticeId-------",String.valueOf(info.noticeId));
				try{
					//if(maxId==null || Integer.valueOf(maxId).intValue()<Integer.valueOf(info.noticeId).intValue()){
					    Cursor c = queryAll(TB_IMPORT_NOTICE,new String[]{FIELD_IMPORT_NOTICE_ID}, new String[]{String.valueOf(info.noticeId)});
					    if(c==null||c.getCount()<1){
					    	Log.e("-----insert-begin-------",info.content);
					    	values = new ContentValues();
					    	values.put(FIELD_IMPORT_NOTICE_ID, info.noticeId);
					    	values.put(FIELD_IMPORT_NOTICE_IMAGE_LOCALURL, info.imageLocalUrl);
					    	values.put(FIELD_IMPORT_NOTICE_IMAGE_WEBURL, info.imageWebUrl);
					    	values.put(FIELD_IMPORT_NOTICE_SEND_TYPE, info.sendType);
					    	values.put(FIELD_IMPORT_NOTICE_APPTYPE, info.appType);
					    	values.put(FIELD_IMPORT_NOTICE_CONTENT, info.content);
					    	values.put(FIELD_IMPORT_NOTICE_ISSUER, info.issuer);
					    	values.put(FIELD_IMPORT_NOTICE_ISSUE_TIME, info.issueTime);
					    	values.put(FIELD_IMPORT_NOTICE_SEND_TIME, info.sendTime);
					    	values.put(FIELD_IMPORT_NOTICE_SENDER, info.sender);
					    	values.put(FIELD_IMPORT_NOTICE_ISREAD, info.isRead);
					    	values.put(FIELD_IMPORT_NOTICE_TOP, info.top);
					    	values.put(FIELD_IMPORT_NOTICE_REPLYCONTENT, info.replyContent);
					    	values.put(FIELD_IMPORT_NOTICE_REPLYTIME, info.replyTime);
					    	db.insert(TB_IMPORT_NOTICE, FIELD_ID, values);
					    }else{
					    	Log.e("-----insert-noticeId-exsist-------",String.valueOf(info.noticeId));
					    }
					//}
				}catch(Exception e){
					Log.e("insert error : ",e.getMessage());
				}
			}
		}
	}
	
	/**
	 * 更新TB_IMPORT_NOTICE_SEND的serverId
	 */
	public void updateServerId(int id,String serverId){
		openDB();
		ContentValues values = new ContentValues();
		values.put(FIELD_IMPORT_NOTICE_SENT_SERVERID, serverId);
		db.update(TB_IMPORT_NOTICE_SEND, values, FIELD_ID + " = ? ", new String[]{String.valueOf(id)});
	}
	
	/**
	 * 得到每日对阵的数据
	 * @param number  哪一天(1-9)
	 * @return
	 */
	public ArrayList<TEverydayAround> getEverydayMatch(int number){
		ArrayList<TEverydayAround> arounds = new ArrayList<TEverydayAround>();
		ArrayList<TEverydayMatch> matches = new ArrayList<TEverydayAround.TEverydayMatch>();;
		Cursor c = null;
		Cursor c1 = null;
		Cursor c2 = null;
		Cursor c3 = null;
		Player a = null;
		Player b = null;
		TEverydayGroup ga = null;
		TEverydayGroup gb = null;
		TEverydayAround around = null;
		TEverydayMatch match = null;
		String score = null;
		String venueId = "";
		try {
			c = queryAll(TB_MATCH, TB_VENUES, FIELD_MATCH_VENUE_ID, FIELD_VENUES_ID, new String[]{FIELD_MATCH_DAY}, new String[]{String.valueOf(number)}, FIELD_VENUES_NUM+","+FIELD_MATCH_NUM+","+FIELD_MATCH_BEGIN_GIME+","+DBHelper.FIELD_MATCH_IS_GROUP);
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				do{
					if(DBHelper.STATUS.MATCH_IS_GROUP_NO.equals(c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_MATCH_IS_GROUP)))){
						c1 = queryOrAll(TB_PLAYER, new String[]{DBHelper.FIELD_PLAYER_ID,DBHelper.FIELD_PLAYER_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_A)),c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_B))});
						if(c1!=null && c1.getCount()==2){
							c1.moveToFirst();
							a = cursorToPlayer(c1);
							c1.moveToNext();
							b = cursorToPlayer(c1);
							if(a.playerId==(c.getInt(c.getColumnIndexOrThrow(FIELD_MATCH_WINNER)))){
								score =  queryScoreByMatchId(c.getString(2),false);
							}
							else{
								score =  queryScoreByMatchId(c.getString(2),true);
							}
							match = new TEverydayMatch(c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_MATCH_TIME)), DBHelper.STATUS.MATCH_IS_GROUP_NO, a, b, 
										c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_STATICS)), 
										c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_RETAIL)), 
										c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_WINNER)), score);
						}
					}
					else if(DBHelper.STATUS.MATCH_IS_GROUP_YES.equals(c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_MATCH_IS_GROUP)))){
						c2 = queryOrAll(TB_PLAYER_GROUP, new String[]{DBHelper.FIELD_PLAYER_GROUP_ID,DBHelper.FIELD_PLAYER_GROUP_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_A)),c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_A))});
						if(c2!=null && c2.getCount()==2){
							c2.moveToFirst();
							do{
								c3  = queryOrAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID,FIELD_PLAYER_ID}, new String[]{c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_A)),c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_B))});
								if(c3!=null && c3.getCount()==2){
									c3.moveToFirst();
									a = cursorToPlayer(c3);
									c3.moveToNext();
									b = cursorToPlayer(c3);
									if(ga==null)
										ga = new TEverydayGroup(c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_ID)), a, b, 
														c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER)), 
														c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_WILDCARD)));
									else
										gb = new TEverydayGroup(c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_ID)), a, b, 
												c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER)), 
												c2.getString(c2.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_WILDCARD)));
								}
							}
							while(c2.moveToNext());
							if(ga.groupId.equals(c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_WINNER)))){
								score = queryScoreByMatchId(c.getString(2),false);
							}
							else{
								score = queryScoreByMatchId(c.getString(2),true);
							}
							if(ga!=null && gb!=null){
								match = new TEverydayMatch(c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_MATCH_TIME)), DBHelper.STATUS.MATCH_IS_GROUP_YES, ga, gb,
										c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_STATICS)), 
										c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_RETAIL)), 
										c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_WINNER)), score);
							}
						}
					}
					if("".equals(venueId) || !venueId.equals(c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_VENUE_ID)))){
						if(!"".equals(venueId))
						{
							around.matches = matches;
							arounds.add(around);
							matches = new ArrayList<TEverydayAround.TEverydayMatch>();
						}
						around = new TEverydayAround(c.getString(c.getColumnIndexOrThrow(FIELD_VENUES_CN_NAME)), c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_BEGIN_GIME)));
						venueId = c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_VENUE_ID));
					}
					if(match!=null){
						matches.add(match);
						a = b = null;
						ga = gb = null;
						match = null;
					}
				}
				while(c.moveToNext());
				if(matches!=null && matches.size()>0){
					around.matches = matches;
					arounds.add(around);
				}
			}
			return arounds;
		} catch (Exception e) {
			Log.w("-----------", e);
			return null;
		}
		finally{
			if(c!=null) c.close();
			if(c1!=null) c1.close();
			if(c2!=null) c2.close();
			if(c3!=null) c3.close();
		}
	}
	
	/*
	 * 通过比赛id去查比分
	 * @param matchId
	 * @param split 分隔分数
	 */
	private String[] queryScoreByMatchId2(String matchId){
		openDB();
		String small = "";
		String[] scores = null;
		Cursor c = null;
		try{
			c = queryAll(TB_SCORE, new String[]{FIELD_SCORE_MATCH_ID}, new String[]{matchId}, FIELD_SCORE_NUM);
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				int len = c.getCount();
				scores = new String[]{"",""};
				boolean flag = false;
				boolean flag1 = false;
				for(int i=0;i<len;i++){
					if(i!=0){
						scores[0]+="  ";
						scores[1]+="  ";
					}
					scores[0]+=c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_A));
					small = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SMALL_SCORE_A));
					if(small!=null && !"".equals(small)){
						scores[0]+="("+small+")";
						flag = true;
					}
					scores[1]+=c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_B));
					small = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SMALL_SCORE_B));
					if(small!=null && !"".equals(small)){
						scores[1]+="("+small+")";
						flag1 = true;
					}
					if(flag || flag1){
						if(!(flag && flag1)){
							if(flag){
								scores[1]+="    ";
							}
							else{
								scores[0]+="    ";
							}
						}
					}
					c.moveToNext();
					flag = false;
				}
			}
			return scores;
		}
		catch(Exception e){
			return null;
		}
		finally{
			if(c!=null) c.close();
		}
	}
	
	
	/*
	 * 通过比赛id去查比分
	 * @param matchId
	 * @param split 分隔分数
	 */
	private String queryScoreByMatchId(String matchId,boolean convert){
		openDB();
		String small = "";
		Cursor c = queryAll(TB_SCORE, new String[]{FIELD_SCORE_MATCH_ID}, new String[]{matchId}, FIELD_SCORE_NUM);
		StringBuffer values  = new StringBuffer();
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			int len = c.getCount();
			for(int i=0;i<len;i++){
				if(i!=0){
					values.append(" ");
				}
				if(!convert){
					values.append(c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_A)));
					small = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SMALL_SCORE_A));
					if(small!=null && !"".equals(small)){
						values.append("(");
						values.append(small);
						values.append(")");
					}
					values.append("-");
					values.append(c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_B)));
					small = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SMALL_SCORE_B));
					if(small!=null && !"".equals(small)){
						values.append("(");
						values.append(small);
						values.append(")");
					}
				}
				else{
					values.append(c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_B)));
					small = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SMALL_SCORE_B));
					if(small!=null && !"".equals(small)){
						values.append("(");
						values.append(small);
						values.append(")");
					}
					values.append("-");
					values.append(c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SCORE_A)));
					small = c.getString(c.getColumnIndexOrThrow(FIELD_SCORE_SMALL_SCORE_A));
					if(small!=null && !"".equals(small)){
						values.append("(");
						values.append(small);
						values.append(")");
					}
				}
				c.moveToNext();
			}
		}
		if(c!=null) c.close();
		return values.toString();
	}
	
	/**
	 * 得到双打签表的数据
	 */
	public TSignTable getDoubleSignTableData(String[] turnIds){
		ArrayList<TSignTableMatch> matches = new ArrayList<TSignTable.TSignTableMatch>();
		Cursor c = null;
		Cursor c1 = null;
		Cursor c2 = null;
		int count = 0;
		Player a = null;
		Player b = null;
		TSignTableGroup ga = null;
		TSignTableGroup gb = null;
		String[] scores = null;
		TSignTableMatch match = null; 
		try{
			c = queryInAll(TB_MATCH,TB_VENUES,FIELD_MATCH_VENUE_ID,FIELD_VENUES_ID,new String[]{FIELD_MATCH_IS_GROUP},new String[]{STATUS.MATCH_IS_GROUP_YES},FIELD_MATCH_TURN_ID,turnIds,FIELD_MATCH_NUM);  
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				do{
					scores = queryScoreByMatchId2(c.getString(2));
					c1 = queryAll(TB_PLAYER_GROUP, new String[]{DBHelper.FIELD_PLAYER_GROUP_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_A))});
					if(c1!=null && c1.getCount()>0){
						c1.moveToFirst();
						c2  = queryAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID}, new String[]{c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_A))});
						if(c2!=null && c2.getCount()>0){
							c2.moveToFirst();
							a = cursorToPlayer(c2);
						}
						c2  = queryAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID}, new String[]{c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_B))});
						if(c2!=null && c2.getCount()>0){
							c2.moveToFirst();
							b = cursorToPlayer(c2);
						}	
						if(!(a==null && b==null))
							ga = new TSignTableGroup(c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_ID)), a, b, 
										c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER)), 
										c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_WILDCARD)));
					}
					c1 = queryAll(TB_PLAYER_GROUP, new String[]{DBHelper.FIELD_PLAYER_GROUP_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_B))});
					if(c1!=null && c1.getCount()>0){
						c1.moveToFirst();
						c2  = queryAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID}, new String[]{c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_A))});
						if(c2!=null && c2.getCount()>0){
							c2.moveToFirst();
							a = cursorToPlayer(c2);
						}
						c2  = queryAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID}, new String[]{c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_B))});
						if(c2!=null && c2.getCount()>0){
							c2.moveToFirst();
							b = cursorToPlayer(c2);
						}	
						if(!(a==null && b==null))
								gb = new TSignTableGroup(c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_ID)), a, b, 
										c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER)), 
										c1.getString(c1.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_GROUP_WILDCARD)));
					}
					
					if(!(ga==null && gb==null)){
						match = new TSignTableMatch(STATUS.MATCH_IS_GROUP_YES, ga, gb, 
								c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_STATICS)), 
								c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_RETAIL)), 
								c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_GROUP_WINNER)), 
								scores==null?null:scores[0], scores==null?null:scores[1], 
								c.getString(c.getColumnIndexOrThrow(FIELD_VENUES_CN_NAME)), 
								c.getString(c.getColumnIndexOrThrow(FIELD_VENUES_EN_NAME)), 
								c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_BEGIN_GIME)));
						matches.add(match);
						count++;
					}
				}while(c.moveToNext());
			}
			return new TSignTable(count, matches);
		}
		catch(Exception e){
			Log.w("--------e-----", e);
			return null;
		}
		finally{
			if(c!=null) c.close();
			if(c1!=null) c1.close();
			if(c2!=null) c2.close();
		}
	}
	
	/**
	 * 得到单打签表的数据
	 */
//	queryInAll(TB_MATCH,TB_VENUES,
//			FIELD_MATCH_VENUE_ID,
//			FIELD_VENUES_ID,
//			new String[]{FIELD_MATCH_IS_GROUP},
//			new String[]{STATUS.MATCH_IS_GROUP_NO},
//			FIELD_MATCH_TURN_ID,
//			turnIds,FIELD_MATCH_NUM); 
	
//	public Cursor queryInAll(String table1,String table2,
//	String field1,String field2,String[] wheres,
//	String[] args,String field,String[] values,String order){
//		openDB();
//		String value = "";
//		String where = "";
//		for(int i=0;i<values.length;i++){
//			if(i!=0) value+=",";
//			value+="'"+values[i]+"'";
//		}
//		for(int i=0;i<wheres.length;i++){
//			if(i>0) where+=QUERY_WHERE_AND;
//			where+=" "+wheres[i]+" = ? ";
//		}
//		where+=" "+QUERY_WHERE_AND+" tb1."+field1+" = tb2."+field2+" "+QUERY_WHERE_AND+" "+field+" in("+value+")";
//		Cursor c = db.query(table1+" as tb1,"+table2+" as tb2 ", null,where,args, null, null, order);
//		if(c!=null)c.moveToFirst();
//		return c;
//	}
	public TSignTable getSingleSignTableData(String[] turnIds){
		ArrayList<TSignTableMatch> matches = new ArrayList<TSignTable.TSignTableMatch>();
		Cursor c = null;
		Cursor c1 = null;
		int count = 0;
		Player a = null;
		Player b = null;
		String[] scores = null;
		TSignTableMatch match = null; 
		try{
			c = queryInAll(TB_MATCH,TB_VENUES,
					FIELD_MATCH_VENUE_ID,
					FIELD_VENUES_ID,
					new String[]{FIELD_MATCH_IS_GROUP},
					new String[]{STATUS.MATCH_IS_GROUP_NO},
					FIELD_MATCH_TURN_ID,
					turnIds,FIELD_MATCH_NUM); 
//			Log.e("Match Count", String.valueOf(c.getCount()));
			
			Cursor ccc = queryAll(TB_MATCH,new String[]{"count(*) as co"});
			ccc.moveToFirst();
			String xx = ccc.getString(ccc.getColumnIndexOrThrow("co"));
			ccc.close();
			Log.e("xxxxxxxxxxxxxxxxxxxxx count score = ",xx);
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				do{
					scores = queryScoreByMatchId2(c.getString(2));
					c1 = queryAll(TB_PLAYER, new String[]{DBHelper.FIELD_PLAYER_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_A))});
					if(c1!=null && c1.getCount()>0){
						c1.moveToFirst();
						a = cursorToPlayer(c1);
					}
					c1 = queryAll(TB_PLAYER, new String[]{DBHelper.FIELD_PLAYER_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_B))});
					if(c1!=null && c1.getCount()>0){	
						c1.moveToFirst();
						b = cursorToPlayer(c1);
					}
					if(!(a==null && b==null)){
						match = new TSignTableMatch(STATUS.MATCH_IS_GROUP_NO, a, b,
									c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_STATICS)), 
									c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_PLAYER_RETAIL)), 
									c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_WINNER)), 
									scores==null?null:scores[0], scores==null?null:scores[1], 
									c.getString(c.getColumnIndexOrThrow(FIELD_VENUES_CN_NAME)), 
							 		c.getString(c.getColumnIndexOrThrow(FIELD_VENUES_EN_NAME)), 
									c.getString(c.getColumnIndexOrThrow(FIELD_MATCH_BEGIN_GIME)));
						matches.add(match);
						count++;
					}
				}
				while(c.moveToNext());
			}
			return new TSignTable(count, matches);
		}
		catch(Exception e){
			Log.w("--------e-----", e);
			return null;
		}
		finally{
			if(c!=null) c.close();
			if(c1!=null) c1.close();
		}
	}
	
	/**
	 * 通过sim卡号来查询人的信息
	 */
	public Person getMeBySim(){
		Person person = null;
		Cursor c = queryAll(TB_PERSON, new String[]{FIELD_PERSON_IMEI}, new String[]{mHelper.getString(Constants.XMPP_USERNAME)});
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			person = new Person(c.getInt(c.getColumnIndexOrThrow(FIELD_ID))
						, c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_ID)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_NAME)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_DEPARTMENT)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_POSITION)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_MOBILE)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_LATITUDE)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_LONGITUDE)), 
						c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_IS_LEADER)));
		}
		if(c!=null) c.close();
		return person;
	}
	
	public void insertImportNoticeSent(int type,String[] personIds,String content,String sendType,String imageLocalUrl,String serverID){
		openDB();
		ContentValues values;
		values = new ContentValues();
		String date = Tools.getCurrentTimeString();
		String meId = "";
		Cursor c = queryAll(TB_PERSON, new String[]{FIELD_PERSON_IMEI}, new String[]{mHelper.getString(Constants.XMPP_USERNAME)});
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			meId = c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_ID));
		}
		if(meId==null || "".equals(meId)) meId = "1";
		values.put(FIELD_IMPORT_NOTICE_SENT_SERVERID,serverID);
		values.put(FIELD_IMPORT_NOTICE_SENT_APPTYPE, type);
		values.put(FIELD_IMPORT_NOTICE_SENT_CONTENT, content);
		values.put(FIELD_IMPORT_NOTICE_SENT_IMAGE_LOCALURL, imageLocalUrl);
		values.put(FIELD_IMPORT_NOTICE_SENT_ISSUE_TIME, date);
		values.put(FIELD_IMPORT_NOTICE_SENT_ISSUER, meId);  
		values.put(FIELD_IMPORT_NOTICE_SENT_SEND_TIME, date);
		values.put(FIELD_IMPORT_NOTICE_SENT_SENDER, meId);
		values.put(FIELD_IMPORT_NOTICE_SENT_SEND_TYPE, sendType);
		long id = db.insert(TB_IMPORT_NOTICE_SEND, FIELD_ID, values);
		int len = personIds!=null?personIds.length:0;
		if(len>0){
			for(int i=0;i<len;i++){
				values = new ContentValues();
				values.put(FIELD_IMPORT_NOTICE_SENT_CHILD_ID, id);
				values.put(FIELD_IMPORT_NOTICE_SENT_CHILD_APPTYPE, type);
				values.put(FIELD_IMPORT_NOTICE_SENT_RECEIVER, personIds[i]);
				db.insert(TB_IMPORT_NOTICE_SEND_CHILD, FIELD_ID, values);
			}
		}else{
			values = new ContentValues();
			values.put(FIELD_IMPORT_NOTICE_SENT_CHILD_ID, id);
			values.put(FIELD_IMPORT_NOTICE_SENT_CHILD_APPTYPE,type);
			values.put(FIELD_IMPORT_NOTICE_SENT_RECEIVER,"0");
			db.insert(TB_IMPORT_NOTICE_SEND_CHILD, FIELD_ID, values);
		}
		if(c!=null) c.close();
	}
	
	public void insertPerson(ArrayList<Person> persons){
		deleteAll(TB_PERSON);
		if(persons!=null){
			openDB();
			ContentValues values;
			for(Person p : persons){
				values = new ContentValues();
				values.put(FIELD_PERSON_ID, p.personId);
				values.put(FIELD_PERSON_DEPARTMENT, p.departmentId);
				values.put(FIELD_PERSON_MOBILE, p.mobile);
				values.put(FIELD_PERSON_NAME, p.name);
				values.put(FIELD_PERSON_POSITION, p.positionId);
				values.put(FIELD_PERSON_IS_LEADER, p.isLeader);
				db.insert(TB_PERSON, FIELD_ID, values);
			}
		}
	}
	
	public void insertDepartment(ArrayList<Department> departments){
		deleteAll(TB_DEPARTMENT);
		if(departments!=null){
			openDB();
			ContentValues values;
			for(Department d : departments){
				values = new ContentValues();
				values.put(FIELD_DEPARTMENT_ID,d.departmentId);
				values.put(FIELD_DEPARTMENT_NAME, d.name);
				db.insert(TB_DEPARTMENT, FIELD_ID, values);
			}
		}
	}
	
	public void insertPosition(ArrayList<Position> positions){
		deleteAll(TB_POSITION);
		if(positions!=null){
			openDB();
			ContentValues values;
			for(Position p : positions){
				values = new ContentValues();
				values.put(FIELD_POSITION_ID,p.positionId);
				values.put(FIELD_POSITION_NAME, p.name);
				db.insert(TB_POSITION, FIELD_ID, values);
			}
		}
	}
	
	public void insertConsult(ArrayList<Consult> consults){
		if(consults!=null){
			openDB();
			ContentValues values;
			for(Consult c : consults){
				values = new ContentValues();
				values.put(FIELD_CONSULT_ID,c.consultId);
				values.put(FIELD_CONSULT_CONTENT, c.content);
				values.put(FIELD_CONSULT_SEND_TIME, c.sendTime);
				db.insert(TB_CONSULT, FIELD_ID, values);
			}
		}
	}
	
	public ArrayList<Person> getPersonByDepartmentIds(String[] departmentIds){
		openDB();
		Cursor c = db.query(TB_PERSON, null, FIELD_PERSON_DEPARTMENT +" in (?)", departmentIds, null, null, null);
		if(c==null || c.getCount()==0) return null;
		c.moveToFirst();
		int len = c.getCount();
		ArrayList<Person> persons = new ArrayList<Person>();
		Person p = null;
 		for(int i=0;i<len;i++){
 			p = new Person();
			p.id = c.getInt(c.getColumnIndexOrThrow(DBHelper.FIELD_ID));
			p.departmentId = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_DEPARTMENT));
			p.latitude = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_LATITUDE));
			p.longitude = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_LONGITUDE));
			p.mobile = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_MOBILE));
			p.name = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME));
			p.personId = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_ID));
			p.positionId = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_POSITION));
			p.isLeader = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_IS_LEADER));
			c.moveToNext();
			persons.add(p);
		}
 		if(c!=null) c.close();
 		return persons;
	}
	
	public ArrayList<Person> getAllPerson(){
		Cursor c = queryAll(TB_PERSON);
		if(c==null || c.getCount()==0) return null;
		c.moveToFirst();
		int len = c.getCount();
		ArrayList<Person> persons = new ArrayList<Person>();
		Person p = null;
		for(int i=0;i<len;i++){
			p = new Person();
			p.id = c.getInt(c.getColumnIndexOrThrow(DBHelper.FIELD_ID));
			p.departmentId = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_DEPARTMENT));
			p.latitude = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_LATITUDE));
			p.longitude = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_LONGITUDE));
			p.mobile = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_MOBILE));
			p.name = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME));
			p.personId = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_ID));
			p.positionId = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_POSITION));
			p.isLeader = c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_IS_LEADER));
			c.moveToNext();
			persons.add(p);
		}
		if(c!=null) c.close();
		return persons;
	}
	
	public ArrayList<Department> getAllDepartment(){
		Cursor c = queryAll(TB_DEPARTMENT);
		if(c==null || c.getCount()==0) return null;
		c.moveToFirst();
		int len = c.getCount();
		ArrayList<Department> departments = new ArrayList<Department>();
		Department d = null;
		for(int i=0;i<len;i++){
			d = new Department();
			d.id = c.getInt(c.getColumnIndexOrThrow(FIELD_ID));
			d.departmentId = c.getString(c.getColumnIndexOrThrow(FIELD_DEPARTMENT_ID));
			d.name = c.getString(c.getColumnIndexOrThrow(FIELD_DEPARTMENT_NAME));
			c.moveToNext();
			departments.add(d);
		}
		if(c!=null) c.close();
		return departments;
	}
	
	/**
	 * 得到单打选手
	 * @return
	 */
	public ArrayList<TPlayer> getSinglePlayer(){
		ArrayList<TPlayer> players = new ArrayList<TPlayer>();
		Cursor c = null;
		int i = 0;
		try{
			c = queryAll(TB_PLAYER);
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				do{
					i++;
					players.add(new TPlayer(String.valueOf(i),cursorToPlayer(c)));
				}
				while(c.moveToNext());
			}
			return players;
		}
		catch(Exception e){
			return null;
		}
		finally{
			if(c!=null) c.close();
		}
	}
	
	private Player cursorToPlayer(Cursor c){
		return new Player(c.getInt(c.getColumnIndexOrThrow(FIELD_ID)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_ID)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_BAT)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_BIRTH_ADDRESS)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_BIRTH_BIRTHDAY)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_CN_NAME)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_CN_SNAME)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_COUNTRY)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_EN_COUNTRY)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_EN_BIRTHADDRESS)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_EN_MEMO)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_EN_NAME)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_EN_PERMANENTADDRESS)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_EN_SNAME)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_FACE)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_HEIGHT)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_PERMANENT_ADDRESS)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_PROFESSIONAL_YEAR)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_SEED_NUMBER)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_WEIGHT)), 
				c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_WILDCARD)));
	}
	
	/**
	 * 得到双打选手
	 * @return
	 */
	public ArrayList<TPlayer> getDoublePlayer(){
		ArrayList<TPlayer> players = new ArrayList<TPlayer>();
		Cursor c = null;
		Cursor c2 = null;
		Player a = null;
		Player b = null;
		TPlayer player = null;
		int i = 0;
		try {
			c = queryAll(TB_PLAYER_GROUP);
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				do{
					c2 = queryOrAll(TB_PLAYER, new String[]{FIELD_PLAYER_ID,FIELD_PLAYER_ID}, new String[]{c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_A)),c.getString(c.getColumnIndexOrThrow(FIELD_PLAYER_GROUP_PLAYER_B))});
					if(c2!=null && c2.getCount()==2){
						c2.moveToFirst();
						a = cursorToPlayer(c2);
						c2.moveToNext();
						b = cursorToPlayer(c2);
						i++;
						player = new TPlayer(String.valueOf(i), a, b);
						players.add(player);
					}
				}
				while(c.moveToNext());
			}
			return players;
		} catch (Exception e) {
			return null;
		}
		finally{
			if(c!=null) c.close();
			if(c2!=null) c2.close();
		}
	}
	
	public void close(){
		if(db!=null) db.close();
	}
	
	public void updateImportNotice(int id,String reply,String replyTime){
		openDB();
		ContentValues values;
		values = new ContentValues();
		Log.e("update is read", String.valueOf(STATUS.IMPORT_NOTICE_READED));
		values.put(FIELD_IMPORT_NOTICE_REPLYCONTENT, reply);
		values.put(FIELD_IMPORT_NOTICE_ISREAD, STATUS.IMPORT_NOTICE_READED);
		values.put(FIELD_IMPORT_NOTICE_REPLYTIME, replyTime);
		int count = db.update(TB_IMPORT_NOTICE, values, FIELD_IMPORT_NOTICE_ID+" = ? ", new String[]{String.valueOf(id)});
		Log.e("----------","updated ["+count+"] row(s)");
	}
	
	public void initUploadReply(){
		Cursor c = queryAll(DBHelper.TB_IMPORT_NOTICE,new String[] { DBHelper.FIELD_IMPORT_NOTICE_ISREAD},new String[] {String.valueOf(STATUS.IMPORT_NOTICE_READED)});
		if(c!=null&&c.getCount()>0){
			c.moveToFirst();
			do{
				ContentValues values = new ContentValues();
				values.put(FIELD_UPLOAD_REPLY_SERVICE_ID,c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_ID)));
				values.put(FIELD_UPLOAD_REPLY_CONTENT,c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_REPLYCONTENT)));
				values.put(FIELD_UPLOAD_REPLY_SEND_TIME,c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_REPLYTIME)));
				Log.i("------------FIELD_UPLOAD_REPLY_SEND_TIME----------",c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_REPLYTIME)));
				db.insert(TB_UPLOAD_REPLY,FIELD_UPLOAD_REPLY_ID, values);
			}while(c.moveToNext());
		}
	}
	
	public void insertUploadReply(String serID,String content,String repTime){
		ContentValues values = new ContentValues();
		values.put(FIELD_UPLOAD_REPLY_SERVICE_ID,serID);
		values.put(FIELD_UPLOAD_REPLY_CONTENT,content);
		values.put(FIELD_UPLOAD_REPLY_SEND_TIME,repTime);
		db.insert(TB_UPLOAD_REPLY,FIELD_UPLOAD_REPLY_ID, values);
		Log.e("----------","insert [1] row(s)");
		Cursor c = queryAll(TB_UPLOAD_REPLY);
		Log.e("----------","upload_reply count " + (c!=null?c.getCount():0));
	}
	
	public Cursor getAllUploadReply(){
		return queryAll(DBHelper.TB_UPLOAD_REPLY,null,null);
	}
	
	public void delUploadReply(String id){
		deleteAll(TB_UPLOAD_REPLY,new String[]{FIELD_UPLOAD_REPLY_SERVICE_ID},new String[]{id});
	}
	
	public String getUploadReply(){
		Cursor c = getAllUploadReply();
		Log.i("---------UPLOAD REPLY COUNT---------",String.valueOf((c!=null?c.getCount():0)));
		if(c!=null&&c.getCount()>0){
			JSONArray js = new JSONArray();
			c.moveToFirst();
			do{
				String serverID = getVal(c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_UPLOAD_REPLY_SERVICE_ID)));
				String content = getVal(c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_UPLOAD_REPLY_CONTENT)));
				String sendTime = getVal(c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_UPLOAD_REPLY_SEND_TIME)));
				js.put(serverID+DataParse.PREX+content+DataParse.PREX+sendTime);
				Log.i("---------UPLOAD REPLY COUNT---------",serverID+DataParse.PREX+content+DataParse.PREX+sendTime);
			}while(c.moveToNext());
			return js.length()>0?js.toString():null;
		}
		return null;
	}
	
	public String getVal(String str){
		return str!=null?str:" ";
	}
	
	public void updateImportNoticeReplys(Reply[] rs){
		openDB();
		ContentValues values;
		for (int i=0;i<rs.length;i++){
				values = new ContentValues();
				values.put(FIELD_IMPORT_NOTICE_SENT_REPLYCONTENT,rs[i].content);
				values.put(FIELD_IMPORT_NOTICE_SENT_REPLYTIME,rs[i].rTime);
				values.put(FIELD_IMPORT_NOTICE_SENT_ISREAD,STATUS.IMPORT_NOTICE_READED);
				int count = db.update(TB_IMPORT_NOTICE_SEND_CHILD, values, FIELD_IMPORT_NOTICE_SENT_CHILD_ID+" = ? and "+FIELD_IMPORT_NOTICE_SENT_RECEIVER+" = ?", new String[]{String.valueOf(rs[i].nID),rs[i].oID});
			    Log.e("----------","updated ["+count+"] row(s)");
		}
	}
	
	
	
	public Weather queryWeather(){
		Cursor c = queryLikeAll(TB_WEATHER,new String[]{DBHelper.FIELD_WEATHER_DATE},new String[]{Tools.getCurrentDateString()});
		Weather w = null;
		if(c!=null && c.getCount()>0){
			Log.e("-------queryWeather Cursor-------",c.getCount()+"");
			c.moveToFirst();
			w = new Weather(c.getInt(c.getColumnIndexOrThrow(FIELD_ID)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_DATE)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_MESSAGE)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD1)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD1)),
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD2)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD2)),
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD3)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD3)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD4)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD4)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD5)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD5)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD6)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD6)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD7)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD7)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD8)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD8)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD9)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD9)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD10)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD10)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD11)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD11)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_PERIOD12)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_PERIOD12)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_HALF1)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_HALF1)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_HALF2)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_HALF2)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_HALF3)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_HALF3)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_TYPE_HALF4)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_STATUS_HALF4)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_UPDATETIME)), 
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_VERSION)),
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_CSTATUS)),
					c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_CURRENTTEMP)));
		}
		return w;
	}
	
//	public void delete()
//	{
//		openDB();
//		db.delete(table, whereClause, whereArgs)；
//	}
	
	public void deleteAll(String table){
		openDB();
		db.delete(table, null, null);
	}
	
	/**
	 * 删除指定的记录
	 * @param table
	 * @return
	 */
	public void deleteAll(String table,String[] fields,String[] values){
		openDB();
		if(fields==null || fields.length==0){
			db.delete(table, null, null);
		}
		else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_AND;
				where+=" "+fields[i]+" = ? ";
			}
			db.delete(table, where, values);
		}
	}
	
	/**
	 * 删除指定的记录
	 * @param table
	 * @return
	 */
	public void deleteLikeAll(String table,String[] fields,String[] values){
		openDB();
		if(fields==null || fields.length==0){
			db.delete(table, null, null);
		}
		else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_AND;
				where+=" "+fields[i]+" like ? ";
			}
			db.delete(table, where, values);
		}
	}
	
	
	/**
	 * 插入比赛
	 * @param matches
	 */
	public void insertMatch(Match[] matches){
		if(matches!=null && matches.length>0){
			openDB();
			db.delete(TB_MATCH, null, null);
			ContentValues values = null;
			for(Match match : matches){
				values = new ContentValues();
				values.put(FIELD_MATCH_IS_GROUP, match.isGroup);
				values.put(FIELD_MATCH_ID, match.matchId);
				values.put(FIELD_MATCH_BEGIN_GIME, match.beginTime);
				values.put(FIELD_MATCH_CN_TITLE, match.cnTitle);
				values.put(FIELD_MATCH_MATCH_TIME, match.matchTime);
				values.put(FIELD_MATCH_DAY, match.day);
				values.put(FIELD_MATCH_MEMO, match.memo);
				values.put(FIELD_MATCH_EN_TITLE, match.enTitle);
				values.put(FIELD_MATCH_IS_SEED, match.isSeed);
				values.put(FIELD_MATCH_SECTION_CN_NAME, match.sectionCnName);
				values.put(FIELD_MATCH_SECTION_EN_NAME, match.sectionEnName);
				values.put(FIELD_MATCH_STATICS, match.statics);
				values.put(FIELD_MATCH_TURN_ID, match.turnId);
				values.put(FIELD_MATCH_VENUE_ID, match.venueId);
				values.put(FIELD_MATCH_PLAYER_A, match.playerA);
				values.put(FIELD_MATCH_PLAYER_RETAIL, match.playerRetail);
				values.put(FIELD_MATCH_PLAYER_B, match.playerB);
				values.put(FIELD_MATCH_WINNER, match.winner);
				values.put(FIELD_MATCH_GROUP_A, match.groupA);
				values.put(FIELD_MATCH_GROUP_RETAIL, match.groupRetail);
				values.put(FIELD_MATCH_GROUP_B, match.groupB);
				values.put(FIELD_MATCH_GROUP_WINNER, match.groupWinner);
				values.put(FIELD_MATCH_EN_MATCH_TIME, match.enMatchTime);
				values.put(FIELD_MATCH_END_TIME, match.endTime);
				values.put(FIELD_MATCH_NUM, match.num);
				db.insert(TB_MATCH, FIELD_ID, values);
			}
		}
		Cursor c = queryAll(TB_MATCH,new String[]{"count(*) as co"});
		c.moveToFirst();
		String xx = c.getString(c.getColumnIndexOrThrow("co"));
		c.close();
		Log.e("xxxxxxxxxxxxxxxxxxxxx count match = ",xx);
	}
	
	/**
	 * 插入比赛
	 * @param matches
	 */
	public void insertMatch(ArrayList<Match> matches){
		if(matches!=null && matches.size()>0){
			openDB();
			db.delete(TB_MATCH, null, null);
			Calendar c = new GregorianCalendar();
			int date = c.get(Calendar.DATE);
			ContentValues values = null;
			deleteAll(TB_MATCH, new String[]{FIELD_MATCH_DAY}, new String[]{String.valueOf(date)});
			for(Match match : matches){
				values = new ContentValues();
				values.put(FIELD_MATCH_IS_GROUP, match.isGroup);
				values.put(FIELD_MATCH_ID, match.matchId);
				values.put(FIELD_MATCH_BEGIN_GIME, match.beginTime);
				values.put(FIELD_MATCH_CN_TITLE, match.cnTitle);
				values.put(FIELD_MATCH_MATCH_TIME, match.matchTime);
				values.put(FIELD_MATCH_DAY, match.day);
				values.put(FIELD_MATCH_MEMO, match.memo);
				values.put(FIELD_MATCH_EN_TITLE, match.enTitle);
				values.put(FIELD_MATCH_IS_SEED, match.isSeed);
				values.put(FIELD_MATCH_SECTION_CN_NAME, match.sectionCnName);
				values.put(FIELD_MATCH_SECTION_EN_NAME, match.sectionEnName);
				values.put(FIELD_MATCH_STATICS, match.statics);
				values.put(FIELD_MATCH_TURN_ID, match.turnId);
				values.put(FIELD_MATCH_VENUE_ID, match.venueId);
				values.put(FIELD_MATCH_PLAYER_A, match.playerA);
				values.put(FIELD_MATCH_PLAYER_RETAIL, match.playerRetail);
				values.put(FIELD_MATCH_PLAYER_B, match.playerB);
				values.put(FIELD_MATCH_WINNER, match.winner);
				values.put(FIELD_MATCH_GROUP_A, match.groupA);
				values.put(FIELD_MATCH_GROUP_RETAIL, match.groupRetail);
				values.put(FIELD_MATCH_GROUP_B, match.groupB);
				values.put(FIELD_MATCH_GROUP_WINNER, match.groupWinner);
				values.put(FIELD_MATCH_EN_MATCH_TIME, match.enMatchTime);
				values.put(FIELD_MATCH_END_TIME, match.endTime);
				db.insert(TB_MATCH, FIELD_ID, values);
			}
		}
		Cursor c = queryAll(TB_MATCH,new String[]{"count(*) as co"});
		c.moveToFirst();
		String xx = c.getString(c.getColumnIndexOrThrow("co"));
		c.close();
		Log.e("xxxxxxxxxxxxxxxxxxxxx count match = ",xx);
	}
	/**
	 * 插入分数
	 * @param scores
	 */
	public void insertScore(Score[] scores){
		if(scores!=null && scores.length>0){
			openDB();
			db.delete(TB_SCORE,null,null);
			ContentValues values = null;
			for(Score score:scores){	
				values = new ContentValues();
				values.put(FIELD_SCORE_ID, score.scoreId);
				values.put(FIELD_SCORE_NUM, score.num);
				values.put(FIELD_SCORE_SCORE_A, score.scoreA);
				values.put(FIELD_SCORE_SCORE_B, score.scoreB);
				values.put(FIELD_SCORE_SMALL_SCORE_A, score.smallScoreA);
				values.put(FIELD_SCORE_SMALL_SCORE_B, score.smallScoreB);
				values.put(FIELD_SCORE_MATCH_ID, score.matchId);
				db.insert(TB_SCORE, FIELD_ID, values);
			}
			
			Cursor c = queryAll(TB_SCORE,new String[]{"count(*) as co"});
			c.moveToFirst();
			String xx = c.getString(c.getColumnIndexOrThrow("co"));
			c.close();
			Log.e("xxxxxxxxxxxxxxxxxxxxx count score = ",xx);
		}
	}
	
	/**
	 * 插入分数
	 * @param scores
	 */
	public void insertScore(ArrayList<Score> scores){
		if(scores!=null && scores.size()>0){
			openDB();
			String ids = "";
			int len = scores.size();
			for(int i=0;i<len;i++){
				if(i!=0) ids+=" or ";
				ids+=FIELD_SCORE_MATCH_ID+" = "+scores.get(i).matchId+" ";
			}
//			db.execSQL("delete from "+TB_SCORE+" where "+ids);
			ContentValues values = null;
			for(Score score:scores){	
				values = new ContentValues();
				values.put(FIELD_SCORE_ID, score.scoreId);
				values.put(FIELD_SCORE_NUM, score.num);
				values.put(FIELD_SCORE_SCORE_A, score.scoreA);
				values.put(FIELD_SCORE_SCORE_B, score.scoreB);
				values.put(FIELD_SCORE_SMALL_SCORE_A, score.smallScoreA);
				values.put(FIELD_SCORE_SMALL_SCORE_B, score.smallScoreB);
				values.put(FIELD_SCORE_MATCH_ID, score.matchId);
				db.insert(TB_SCORE, FIELD_ID, values);
			}
			Cursor c = queryAll(TB_SCORE,new String[]{"count(*) as co"});
			c.moveToFirst();
			String xx = c.getString(c.getColumnIndexOrThrow("co"));
			c.close();
			Log.e("xxxxxxxxxxxxxxxxxxxxx count score = ",xx);
		}
	}
	
	/**
	 * 保存观众意见
	 * @param consult
	 */
	public void saveConsult(Consult consult){
		if(consult!=null){
			openDB();
			ContentValues values = new ContentValues();
			values.put(FIELD_CONSULT_ID, consult.consultId);
			values.put(FIELD_CONSULT_CONTENT, consult.content);
			values.put(FIELD_CONSULT_SEND_TIME, consult.sendTime);
			db.insert(TB_CONSULT, FIELD_ID, values);
		}
	}
	
	/**
	 * 保存多条观从意见
	 * @param consults
	 */
	public void saveConsuts(ArrayList<Consult> consults){
		if(consults!=null && consults.size()>0){
			for(Consult consult:consults){
				saveConsult(consult);
			}
		}
	}
	
	public String getCurrentDateVersion(){
		try{
			Log.e("-------Get Weather Version Today : -------",Tools.getCurrentDateString());
			Cursor c = queryLikeAll(TB_WEATHER,new String[]{DBHelper.FIELD_WEATHER_DATE},new String[]{Tools.getCurrentDateString()});
			Log.e("-------Get Weather Version Today Count : -------",(c!=null?c.getCount()+"":"0"));
			if(c!=null && c.getCount()>0){
				c.moveToFirst();
				String version = c.getString(c.getColumnIndexOrThrow(FIELD_WEATHER_VERSION));
				Log.e("-------Get Weather Version Today Version : -------",version);
				c.close();
				if(version!=null&&!"".equals(version)&&Integer.valueOf(version).intValue()>0){
					return version;
				}else{
					return "-2";
				}
			}
		}catch(Exception e){
			MyLog.error(DBHelper.class,"",e);
		}
		return "-2";
	}
	
	public String GetPlayMaxID(){
		String strMaxID = "0";
		strMaxID = this.getMaxId(TB_PLAYER, FIELD_PLAYER_ID);		
		return strMaxID;
	}
	
	public String GetPlayerGroupSql(){
		String strPlayerSql = Constants.PLAYERGROUP;
		strPlayerSql = strPlayerSql + " where T0.ID > " + "0";//this.getMaxId(TB_PLAYER_GROUP,FIELD_PLAYER_GROUP_ID);
		return strPlayerSql;
	}
	
	public String GetPlayerSql(){
		String strPlayerSql = Constants.PLAYER;
		strPlayerSql = strPlayerSql + " where T0.ID > " + "0";//this.GetPlayMaxID();
		return strPlayerSql;
	}
	
	public void savePlayerGroups(PlayerGroup[] ps){
		this.deleteAll(TB_PLAYER_GROUP);
		Log.e("-----insert players group length : -------",""+ps.length);
		openDB();
		ContentValues values;
		for (int i=0;i<ps.length;i++) {
			PlayerGroup p = ps[i];
			try{
				values = new ContentValues();
				values.put(FIELD_PLAYER_GROUP_PLAYER_A, p.playerA);
				values.put(FIELD_PLAYER_GROUP_PLAYER_B, p.playerB);
				values.put(FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER, p.groupSeedNum);
				values.put(FIELD_PLAYER_GROUP_GROUP_WILDCARD, p.groupWildCard);
				db.insert(TB_PLAYER_GROUP, FIELD_ID, values);	
			}catch(Exception e){
				Log.e("insert player group","error : ",e);
			}
		}

	}
	
	public void savePlayers(Player[] ps){
		this.deleteAll(TB_PLAYER);
		Log.e("-----insert players length : -------",""+ps.length);
			openDB();
			ContentValues values;
			for (int i=0;i<ps.length;i++) {
				Player p = ps[i];
				try{
					values = new ContentValues();
					values.put(FIELD_PLAYER_ID, p.playerId);
					values.put(FIELD_PLAYER_BAT, p.bat);
					values.put(FIELD_PLAYER_BIRTH_ADDRESS, p.birthAddress);
					values.put(FIELD_PLAYER_BIRTH_BIRTHDAY, p.birthday);
					values.put(FIELD_PLAYER_BOUNS, p.bonus);
					values.put(FIELD_PLAYER_CN_NAME, p.cnName);
					values.put(FIELD_PLAYER_CN_SNAME, p.cnSname);
					values.put(FIELD_PLAYER_COUNTRY, p.country);
					values.put(FIELD_PLAYER_EN_COUNTRY, p.enCountry);
					values.put(FIELD_PLAYER_EN_BIRTHADDRESS, p.enBirthAddress);
					values.put(FIELD_PLAYER_EN_MEMO, p.enMemo);
					values.put(FIELD_PLAYER_EN_NAME, p.enName);
					values.put(FIELD_PLAYER_EN_PERMANENTADDRESS, p.enPermanentAddress);
					values.put(FIELD_PLAYER_EN_SNAME, p.enSname);
					values.put(FIELD_PLAYER_FACE, p.face);
					values.put(FIELD_PLAYER_HEIGHT, p.hegith);
					values.put(FIELD_PLAYER_LAST_RACE_FAIL, p.lastRaceFail);
					values.put(FIELD_PLAYER_LAST_RACE_WIN, p.lastRaceWin);
					values.put(FIELD_PLAYER_LAST_TURN_COUNT, p.lastTurnCount);
					values.put(FIELD_PLAYER_LAST_TURN_DATE, p.lastTurnDate);
					values.put(FIELD_PLAYER_MEMO, p.memo);
					values.put(FIELD_PLAYER_NEWS_RACE_FAIL, p.newsRaceFail);
					values.put(FIELD_PLAYER_NEWS_RACE_WIN, p.newsRaceWin);
					values.put(FIELD_PLAYER_NUM, p.num);
					values.put(FIELD_PLAYER_PERMANENT_ADDRESS, p.permanentAddress);
					values.put(FIELD_PLAYER_PROFESSIONAL_YEAR, p.professionalYear);
					values.put(FIELD_PLAYER_RACE_CHAM_COUNT, p.raceChamCount);
					values.put(FIELD_PLAYER_SEED_NUMBER, p.seedNumber);
					values.put(FIELD_PLAYER_WEIGHT, p.weight);
					values.put(FIELD_PLAYER_GAME_ID, p.gameId);
					values.put(FIELD_PLAYER_WILDCARD, p.wildCard);		
					db.insert(TB_PLAYER, FIELD_ID, values);	
				}catch(Exception e){
					Log.e("insert player","error : ",e);
				}
			}
	}
	
	
	public void savePlayer(Player p)
	{
		if(p != null)
		{
			openDB();
			ContentValues values = new ContentValues();
			values.put(FIELD_PLAYER_ID, p.playerId);
			values.put(FIELD_PLAYER_BAT, p.bat);
			values.put(FIELD_PLAYER_BIRTH_ADDRESS, p.birthAddress);
			values.put(FIELD_PLAYER_BIRTH_BIRTHDAY, p.birthday);
			values.put(FIELD_PLAYER_BOUNS, p.bonus);
			values.put(FIELD_PLAYER_CN_NAME, p.cnName);
			values.put(FIELD_PLAYER_CN_SNAME, p.cnSname);
			values.put(FIELD_PLAYER_COUNTRY, p.country);
			values.put(FIELD_PLAYER_EN_COUNTRY, p.enCountry);
			values.put(FIELD_PLAYER_EN_BIRTHADDRESS, p.enBirthAddress);
			values.put(FIELD_PLAYER_EN_MEMO, p.enMemo);
			values.put(FIELD_PLAYER_EN_NAME, p.enName);
			values.put(FIELD_PLAYER_EN_PERMANENTADDRESS, p.enPermanentAddress);
			values.put(FIELD_PLAYER_EN_SNAME, p.enSname);
			values.put(FIELD_PLAYER_FACE, p.face);
			values.put(FIELD_PLAYER_HEIGHT, p.hegith);
			values.put(FIELD_PLAYER_LAST_RACE_FAIL, p.lastRaceFail);
			values.put(FIELD_PLAYER_LAST_RACE_WIN, p.lastRaceWin);
			values.put(FIELD_PLAYER_LAST_TURN_COUNT, p.lastTurnCount);
			values.put(FIELD_PLAYER_LAST_TURN_DATE, p.lastTurnDate);
			values.put(FIELD_PLAYER_MEMO, p.memo);
			values.put(FIELD_PLAYER_NEWS_RACE_FAIL, p.newsRaceFail);
			values.put(FIELD_PLAYER_NEWS_RACE_WIN, p.newsRaceWin);
			values.put(FIELD_PLAYER_NUM, p.num);
			values.put(FIELD_PLAYER_PERMANENT_ADDRESS, p.permanentAddress);
			values.put(FIELD_PLAYER_PROFESSIONAL_YEAR, p.professionalYear);
			values.put(FIELD_PLAYER_RACE_CHAM_COUNT, p.raceChamCount);
			values.put(FIELD_PLAYER_SEED_NUMBER, p.seedNumber);
			values.put(FIELD_PLAYER_WEIGHT, p.weight);
			values.put(FIELD_PLAYER_GAME_ID, p.gameId);
			values.put(FIELD_PLAYER_WILDCARD, p.wildCard);		
			
			db.insert(TB_PLAYER, FIELD_ID, values);	
			
		}
	}
	
	
	
	/**
	 * 保存天气预报
	 * @param w
	 */
	public void saveWeather(Weather w){
		Log.e("-------saveWeather-------",String.valueOf(w));
		if(w!=null){
			openDB();
			/* 删除当天的天气情况 */
			deleteLikeAll(TB_WEATHER,new String[]{DBHelper.FIELD_WEATHER_DATE},new String[]{Tools.getCurrentDateString()});
			ContentValues values = new ContentValues();
			values.put(FIELD_WEATHER_DATE, w.date);
			values.put(FIELD_WEATHER_MESSAGE, w.message);
			values.put(FIELD_WEATHER_TYPE_PERIOD1, w.typePeriod1);
			values.put(FIELD_WEATHER_STATUS_PERIOD1, w.statusPeriod1);
			values.put(FIELD_WEATHER_TYPE_PERIOD2, w.typePeriod2);
			values.put(FIELD_WEATHER_STATUS_PERIOD2, w.statusPeriod2);
			values.put(FIELD_WEATHER_TYPE_PERIOD3, w.typePeriod3);
			values.put(FIELD_WEATHER_STATUS_PERIOD3, w.statusPeriod3);
			values.put(FIELD_WEATHER_TYPE_PERIOD4, w.typePeriod4);
			values.put(FIELD_WEATHER_STATUS_PERIOD4, w.statusPeriod4);
			values.put(FIELD_WEATHER_TYPE_PERIOD5, w.typePeriod5);
			values.put(FIELD_WEATHER_STATUS_PERIOD5, w.statusPeriod5);
			values.put(FIELD_WEATHER_TYPE_PERIOD6, w.typePeriod6);
			values.put(FIELD_WEATHER_STATUS_PERIOD6, w.statusPeriod6);
			values.put(FIELD_WEATHER_TYPE_PERIOD7, w.typePeriod7);
			values.put(FIELD_WEATHER_STATUS_PERIOD7, w.statusPeriod7);
			values.put(FIELD_WEATHER_TYPE_PERIOD8, w.typePeriod8);
			values.put(FIELD_WEATHER_STATUS_PERIOD8, w.statusPeriod8);
			values.put(FIELD_WEATHER_TYPE_PERIOD9, w.typePeriod9);
			values.put(FIELD_WEATHER_STATUS_PERIOD9, w.statusPeriod9);
			values.put(FIELD_WEATHER_TYPE_PERIOD10, w.typePeriod10);
			values.put(FIELD_WEATHER_STATUS_PERIOD10, w.statusPeriod10);
			values.put(FIELD_WEATHER_TYPE_PERIOD11, w.typePeriod11);
			values.put(FIELD_WEATHER_STATUS_PERIOD11, w.statusPeriod11);
			values.put(FIELD_WEATHER_TYPE_PERIOD12, w.typePeriod12);
			values.put(FIELD_WEATHER_STATUS_PERIOD12, w.statusPeriod12);
			values.put(FIELD_WEATHER_TYPE_HALF1, w.typeHalf1);
			values.put(FIELD_WEATHER_STATUS_HALF1, w.statusHalf1);
			values.put(FIELD_WEATHER_TYPE_HALF2, w.typeHalf2);
			values.put(FIELD_WEATHER_STATUS_HALF2, w.statusHalf2);
			values.put(FIELD_WEATHER_TYPE_HALF3, w.typeHalf3);
			values.put(FIELD_WEATHER_STATUS_HALF3, w.statusHalf3);
			values.put(FIELD_WEATHER_TYPE_HALF4, w.typeHalf4);
			values.put(FIELD_WEATHER_STATUS_HALF4, w.statusHalf4);
			values.put(FIELD_WEATHER_UPDATETIME, w.updateTime);
			values.put(FIELD_WEATHER_VERSION, w.version);
			values.put(FIELD_WEATHER_CURRENTTEMP, w.temperature);
			values.put(FIELD_WEATHER_CSTATUS, w.currentStatus);
			db.insert(TB_WEATHER, FIELD_ID, values);			
		}
		
	}
	
	public Cursor queryAll(String table){
		openDB();
		Cursor c = db.query(table, null, null, null, null, null, null);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public String getPersonName(String id){
		Cursor c = queryAll(TB_PERSON, new String[]{FIELD_PERSON_ID}, new String[]{id});
		if(c!=null && c.getCount()>0){
			c.moveToFirst();
			return c.getString(c.getColumnIndexOrThrow(FIELD_PERSON_NAME));
		}
		return "";
	}
	
	public Cursor queryAll(String table1,String table2,String field1,String field2,String[] fields,String[] values,String order){
		openDB();
		String where = "";
		for(int i=0;i<fields.length;i++){
			if(i>0) where+=QUERY_WHERE_AND;
			where+=" "+fields[i]+" = ? ";
		}
		where+=" "+QUERY_WHERE_AND+" tb1"+"."+field1+" = tb2"+"."+field2;
		Cursor c = db.query(table1+" as tb1,"+table2+" as tb2 ", null, where, values, null, null, order);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryAll(String table1,String table2,String field1,String field2,String[] fields,String[] values,String order,String limit){
		openDB();
		String where = "";
		for(int i=0;i<fields.length;i++){
			if(i>0) where+=QUERY_WHERE_AND;
			where+=" "+fields[i]+" = ? ";
		}
		where+=" "+QUERY_WHERE_AND+" tb1"+"."+field1+" = tb2"+"."+field2;
		Cursor c = db.query(table1+" as tb1,"+table2+" as tb2 ", null, where, values, null, null, order,limit);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryAll(String table1,String table2,String field1,String field2,String[] fields,String[] values){
		openDB();
		String where = "";
		for(int i=0;i<fields.length;i++){
			if(i>0) where+=QUERY_WHERE_AND;
			where+=" "+fields[i]+" = ? ";
		}
		where+=" "+QUERY_WHERE_AND+" tb1."+field1+" = tb2."+field2;
		Cursor c = db.query(table1+" as tb1,"+table2+" as tb2 ", null, where, values, null, null, null);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryAll(String table,String[] fields){
		openDB();
		Cursor c = db.query(table, fields, null, null, null, null, null);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	
	public Cursor queryOrAll(String table,String[] fields,String[] values){
		openDB();
		Cursor c = null;
		if(fields==null || fields.length==0){
			c = db.query(table, null, null, null, null, null, null);
		}else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_OR;
				where+=" "+fields[i]+" = ? ";
			}
			Log.d("------------", where);
			c = db.query(table, null, where, values, null, null,null);
		}
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryNotAll(String table,String[] fields,String[] values){
		openDB();
		Cursor c = null;
		if(fields==null || fields.length==0){
			c = db.query(table, null, null, null, null, null, null);
		}else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_OR;
				where+=" "+fields[i]+" != ? ";
			}
			Log.d("------------", where);
			c = db.query(table, null, where, values, null, null,null);
		}
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryAll(String table,String[] fields,String[] values){
		openDB();
		Cursor c = null;
		if(fields==null || fields.length==0){
			c = db.query(table, null, null, null, null, null, null);
		}else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_AND;
				where+=" "+fields[i]+" = ? ";
			}
			Log.d("------------", where);
			c = db.query(table, null, where, values, null, null,null);
		}
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryAll(String table,String[] fields,String[] values,String order){
		openDB();
		Cursor c = null;
		if(fields==null || fields.length==0){
			c = db.query(table, null, null, null, null, null, null);
		}else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_AND;
				where+=" "+fields[i]+" = ? ";
			}
			c = db.query(table, null, where, values, null, null,order);
		}
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryAll(String table,String[] columns,String[] fields,String[] values){
		openDB();
		Cursor c = null;
		if(fields==null || fields.length==0){
			c = db.query(table, null, null, null, null, null, null);
		}else{
			String where = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_AND;
				where+=" "+fields[i]+" = ? ";
			}
			c = db.query(table, columns, where, values, null, null,null);
		}
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryInAll(String table,String fields,String[] values,String order){
		openDB();
		Cursor c = null;
		String value = "";
		for(int i=0;i<values.length;i++){
			if(i!=0) value+=",";
			value+="'"+values[i]+"'";
		}
		c = db.query(table, null, fields+" in("+value+") ",null, null, null, null);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryInAll(String table1,String table2,String field1,String field2,String[] wheres,
			String[] args,String field,String[] values,String order){
		openDB();
		String value = "";
		String where = "";
		for(int i=0;i<values.length;i++){
			if(i!=0) value+=",";
			value+="'"+values[i]+"'";
		}
		for(int i=0;i<wheres.length;i++){
			if(i>0) where+=QUERY_WHERE_AND;
			where+=" "+wheres[i]+" = ? ";
		}
		where+=" "+QUERY_WHERE_AND+" tb1."+field1+" = tb2."+field2+" "+QUERY_WHERE_AND+" "+field+" in("+value+")";
		Cursor c = db.query(table1+" as tb1,"+table2+" as tb2 ", null,where,args, null, null, order);
		if(c!=null)c.moveToFirst();
		return c;
	}
	
	public Cursor queryLikeAll(String table,String[] fields,String[] values){
		openDB();
		Cursor c = null;
		if(fields==null || fields.length==0){
			c = db.query(table, null, null, null, null, null, null);
		}else{
			String where = "";
			String vals = "";
			for(int i=0;i<fields.length;i++){
				if(i>0) where+=QUERY_WHERE_AND;
				where+=" "+fields[i]+" like ? ";
				vals += values[i];
			}
			c = db.query(table, null, where, values, null, null,null);
			Log.e("-----sql like --------",where+vals);
		}
		if(c!=null)c.moveToFirst();
		Log.e("-----sql like res count--------",String.valueOf((c!=null?c.getCount():0)));
		return c;
	}
	
	public Cursor nearlyNotice(){
		String where = " "+DBHelper.FIELD_IMPORT_NOTICE_ISREAD+" = ? and ("+DBHelper.FIELD_IMPORT_NOTICE_SEND_TIME+" like ? or "+DBHelper.FIELD_IMPORT_NOTICE_SEND_TIME+" like ?)";
		Cursor c = queryLikeAll(DBHelper.TB_IMPORT_NOTICE,where,new String[] {String.valueOf(STATUS.IMPORT_NOTICE_UN_READ),Tools.getAlertDateString(0)+"%",Tools.getAlertDateString(-1)+"%"}," order by "+DBHelper.FIELD_IMPORT_NOTICE_ID+" desc");
		if(c!=null&&c.getCount()>0){
			c.moveToFirst();
			do{
				String rTime = c.getString(c.getColumnIndex(DBHelper.FIELD_IMPORT_NOTICE_SEND_TIME));
				if(Tools.isNear(rTime)){
					return queryAll(DBHelper.TB_IMPORT_NOTICE,new String[]{FIELD_ID},new String[]{c.getString(c.getColumnIndex(FIELD_ID))});
				}
			}while(c.moveToNext());
		}
		return null;
	}
	
	public Cursor queryLikeAll(String table,String where,String[] values,String order){
		openDB();
		Cursor c = null;
			String vals = "";
			for(int i=0;i<values.length;i++){
				vals += " : "+values[i];
			}
			c = db.query(table, null, where, values, null, null,null);
			Log.e("-----sql like --------",where+vals);
		if(c!=null)c.moveToFirst();
		Log.e("-----sql like res --------",String.valueOf(c!=null?c.getCount():0));
		//test();
		return c;
	}
	
	public void test(){
		Cursor c = queryAll(TB_IMPORT_NOTICE);
		if(c!=null&&c.getCount()>0){
			do{
				Log.e("---------------TB_IMPORT_NOTICE----------------",String.valueOf(c.getString(c.getColumnIndex(FIELD_IMPORT_NOTICE_ISREAD))) + " : "+ String.valueOf(c.getString(c.getColumnIndex(FIELD_IMPORT_NOTICE_SEND_TIME))));
			}while(c.moveToNext());
		}
	}
	
	public String getIsReadString(int type){
		switch(type){
			case STATUS.IMPORT_NOTICE_UN_READ:
				return "未读";
			case STATUS.IMPORT_NOTICE_READED:
				return "已读";
		}
		return "";
	}
    
	public String getMyName(String mobileID){
		String name = "";
		Cursor c = queryAll(TB_PERSON,new String[]{FIELD_PERSON_IMEI},new String[]{mobileID});
		if(c!=null&&c.getCount()>0){
			c.moveToFirst();
			name = c.getString(c.getColumnIndex(FIELD_PERSON_NAME));
			Log.e("----------name:",name);
			c.close();
		}
		return name;
	}
	
	public static interface STATUS{
		/**
		 * 重要通告未读状态
		 */
		public final static int IMPORT_NOTICE_UN_READ = 0;
		/**
		 * 重要通告已读状态
		 */
		public final static int IMPORT_NOTICE_READED = 1;
		
		public final static String LEADER_HIGHT = "0";   //高层领导
		public final static String LEADER_MIDDLE = "1";  //中层领导
		public final static String LEADER_SMALL = "2";   //普通人员
		
		public final static String MATCH_IS_GROUP_YES = "YES";  
		public final static String MATCH_IS_GROUP_NO = "NO";
	}

	public static final String QUERY_WHERE_ARG = " like ? ";
	public static final String QUERY_WHERE_OR= " or ";
	public static final String QUERY_WHERE_AND = " and ";
	public static final String QUERY_SPACE = " ? ";
	public static final String TB_DELETE = "DROP TABLE IF EXISTS ";

	private final static String DATABASE_NAME = "eipc.db";
	private final static int DATABASE_VERSION = 1;
	/* 表名 */
	public final static String TB_IMPORT_NOTICE = "import_notice";//重要通告
	public final static String TB_PERSON = "person";    //人员表
	public final static String TB_PERSON2 = "person2";    //人员表（领导使用）
	public final static String TB_DEPARTMENT = "department";  //部门表
	public final static String TB_POSITION = "position";   //职责
	
	// add
	public final static String TB_IMPORT_NOTICE_SEND = "import_notice_send";//重要通告(已发送)
	public final static String TB_IMPORT_NOTICE_SEND_CHILD = "import_notice_send_child";//重要通告(已发送)
	
	public final static String TB_WEATHER = "weather";   //天气预报
	public final static String TB_PLAYER = "player";     //球员信息表
	public final static String TB_MATCH = "match";       //比赛表
	public final static String TB_PLAYER_GROUP = "player_group"; //双打组表
	public final static String TB_TURN = "turn";       //轮次表
	public final static String TB_DATASTATICSTICS = "datastaticstics"; //数据统计表
	public final static String TB_GAMES = "games";   //赛事表
	public final static String TB_TOTAL = "total";   //总分表
	public final static String TB_SENTENCE_SCORE = "sentence_score";//局分表
	public final static String TB_VENUES = "venues"; //场馆表
	public final static String TB_SCORE = "score";    //盘分数表
	public final static String TB_CONSULT = "consult"; //咨询服务中心
	public final static String TB_UPLOAD_REPLY = "upload_reply"; //回复同步表
	public final static String TB_LATEST_SCORE = "latestScore"; //最新比分
	
	
	
	public void delete1006(){
		try{
			openDB();
			db.delete(TB_IMPORT_NOTICE, null, null);
			db.delete(TB_IMPORT_NOTICE_SEND, null, null);
			db.delete(TB_MATCH, null, null);
			db.delete(TB_SCORE, null, null);
			db.delete(TB_CONSULT, null, null);
			db.delete(TB_LATEST_SCORE, null, null);
		}catch(Exception e){
			
		}
		
	}
	
		
	
	/* 字段 */
	public final static String FIELD_ID = "_id";	
	
	/* 重要通告 (接收到的,别人发送过来的)*/
	//这个ID存服务器发送过来的ID，所以不是流水号，可能不连续
	public final static String FIELD_IMPORT_NOTICE_ID= "id";
	// 功能类型
	public final static String FIELD_IMPORT_NOTICE_APPTYPE= "appType";
	public final static String FIELD_IMPORT_NOTICE_CONTENT = "content";
	public final static String FIELD_IMPORT_NOTICE_IMAGE_WEBURL = "image_weburl";
	public final static String FIELD_IMPORT_NOTICE_IMAGE_LOCALURL = "image_localurl";
	public final static String FIELD_IMPORT_NOTICE_ISSUE_TIME = "issue_time";
	public final static String FIELD_IMPORT_NOTICE_ISSUER = "issuer";
	public final static String FIELD_IMPORT_NOTICE_SEND_TIME = "send_time";
	public final static String FIELD_IMPORT_NOTICE_SENDER = "sender";
	public final static String FIELD_IMPORT_NOTICE_SEND_TYPE = "send_type";
	public final static String FIELD_IMPORT_NOTICE_TOP = "top";
	// 本终端的回复情况
	public final static String FIELD_IMPORT_NOTICE_ISREAD = "isread";
	public final static String FIELD_IMPORT_NOTICE_REPLYCONTENT = "replyContent";
	public final static String FIELD_IMPORT_NOTICE_REPLYTIME = "replyTime";
	
	//重要通知获取方式 0:push; 1:轮询; 2:其它
	//public final static String FIELD_IMPORT_NOTICE_TYPE = "noticeType";
	
	/* 重要通告(已发送，我发送给别人的) */
	// 本地发送的重要通知的序列号，流水号
//	public final static String FIELD_IMPORT_NOTICE_SENT_ID= "id";
	// 功能类型
	public final static String FIELD_IMPORT_NOTICE_SENT_APPTYPE= "appType";
	// 该通知在服务器端的ID号，若为空则代表未发送，否则为已发送
	public final static String FIELD_IMPORT_NOTICE_SENT_SERVERID= "serverid";
	public final static String FIELD_IMPORT_NOTICE_SENT_CONTENT = "content";
	public final static String FIELD_IMPORT_NOTICE_SENT_IMAGE_LOCALURL = "image_localurl";
	// 这里签发和发送的字段都加上，实际填充的时候都是该终端的使用者
	public final static String FIELD_IMPORT_NOTICE_SENT_ISSUE_TIME = "issue_time";
	public final static String FIELD_IMPORT_NOTICE_SENT_ISSUER = "issuer";
	public final static String FIELD_IMPORT_NOTICE_SENT_SEND_TIME = "send_time";
	public final static String FIELD_IMPORT_NOTICE_SENT_SENDER = "sender";
	public final static String FIELD_IMPORT_NOTICE_SENT_SEND_TYPE = "send_type";
	
	/* 重要通告(已发送子表，记录每条信息的接受者和回复状态) ；注意：本张表暂时只使用前面两个字段
	 * 查看接受者状态的实现，都去读服务器URL*/	
	// 本地发送的重要通知的序列号，流水号；用于与主表关联
	public final static String FIELD_IMPORT_NOTICE_SENT_CHILD_ID= "child_id";
	// 功能类型
	public final static String FIELD_IMPORT_NOTICE_SENT_CHILD_APPTYPE= "appType";
	// 接受者的电话号码
	public final static String FIELD_IMPORT_NOTICE_SENT_RECEIVER= "receiver";
	public final static String FIELD_IMPORT_NOTICE_SENT_ISREAD = "isread";
	public final static String FIELD_IMPORT_NOTICE_SENT_REPLYCONTENT = "ReplyContent";
	public final static String FIELD_IMPORT_NOTICE_SENT_REPLYTIME = "ReplyTime";

	
	/* 人员表 */
	public final static String FIELD_PERSON_ID = "id";
	public final static String FIELD_PERSON_NAME = "name";
	public final static String FIELD_PERSON_DEPARTMENT = "departmentId";
	public final static String FIELD_PERSON_POSITION = "positionId";
	public final static String FIELD_PERSON_MOBILE = "mobile";
	public final static String FIELD_PERSON_IMEI = "imei";
	public final static String FIELD_PERSON_SHORT_MOBILE = "short_mobile";
	public final static String FIELD_PERSON_IS_LEADER = "is_leader";
	
	
	/* 人员表2 */
	public final static String FIELD_PERSON2_ID = "id";
	public final static String FIELD_PERSON2_NAME = "name";
	public final static String FIELD_PERSON2_DEPARTMENT = "departmentId";
	public final static String FIELD_PERSON2_POSITION = "positionId";
	public final static String FIELD_PERSON2_MOBILE = "mobile";
	public final static String FIELD_PERSON2_IMEI = "imei";
	public final static String FIELD_PERSON2_SHORT_MOBILE = "short_mobile";
	public final static String FIELD_PERSON2_IS_LEADER = "is_leader";
	
	// 查看人员位置时的GPS坐标
	public final static String FIELD_PERSON_LATITUDE="geoLatitude";
	public final static String FIELD_PERSON_LONGITUDE="geoLongitude";
	
	/* 部门表 */
	public final static String FIELD_DEPARTMENT_ID = "id";
	public final static String FIELD_DEPARTMENT_NAME = "name";
	/* 职责表 */
	public final static String FIELD_POSITION_ID = "id";
	public final static String FIELD_POSITION_NAME = "name";

	/* 天气预报 */
	public final static String FIELD_WEATHER_DATE = "date";//哪天的天气
	//关于天气的及时信息，该信息在当日天气上方显示，仅1行，滚动显示，若无则不显示
	public final static String FIELD_WEATHER_MESSAGE = "message";  
	// 当日分时天气
	// TYPE定义：
	// 1晴 2多云 3阴 4阵雨 5雷阵雨 6小雨 7中雨 8大雨 
	// 9暴雨 10大暴雨 11特大暴雨 12小雨-中雨 13中雨-大雨 
	// 14大雨-暴雨 15暴雨-大暴雨 16大暴雨-特大暴雨
	// 为减少接口字段，这里存数字（16种类型是定死的）
	public final static String FIELD_WEATHER_TYPE_PERIOD1 = "type_peroid1"; //0~2时
	// STATUS定义:
	// 关于风力和温度的字符串；与TYPE合起来为每行显示内容
	public final static String FIELD_WEATHER_STATUS_PERIOD1 = "status_peroid1"; //0~2时
	public final static String FIELD_WEATHER_TYPE_PERIOD2 = "type_peroid2"; //2~4时
	public final static String FIELD_WEATHER_STATUS_PERIOD2 = "status_peroid2"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD3 = "type_peroid3"; //4~6时
	public final static String FIELD_WEATHER_STATUS_PERIOD3 = "status_peroid3"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD4 = "type_peroid4";//6~8时 
	public final static String FIELD_WEATHER_STATUS_PERIOD4 = "status_peroid4"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD5 = "type_peroid5";//8~10时 
	public final static String FIELD_WEATHER_STATUS_PERIOD5 = "status_peroid5"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD6 = "type_peroid6";//10~12时 
	public final static String FIELD_WEATHER_STATUS_PERIOD6 = "status_peroid6"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD7 = "type_peroid7";//12~14时 
	public final static String FIELD_WEATHER_STATUS_PERIOD7 = "status_peroid7"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD8 = "type_peroid8";//14~16时 
	public final static String FIELD_WEATHER_STATUS_PERIOD8 = "status_peroid8"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD9 = "type_peroid9";//16~18时 
	public final static String FIELD_WEATHER_STATUS_PERIOD9 = "status_peroid9"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD10 = "type_peroid10";//18~20时 
	public final static String FIELD_WEATHER_STATUS_PERIOD10 = "status_peroid10"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD11 = "type_peroid11";//20~22时 
	public final static String FIELD_WEATHER_STATUS_PERIOD11 = "status_peroid11"; 
	public final static String FIELD_WEATHER_TYPE_PERIOD12 = "type_peroid12";//22~24时 
	public final static String FIELD_WEATHER_STATUS_PERIOD12 = "status_peroid12";
	// 未来两天天气情况
	public final static String FIELD_WEATHER_TYPE_HALF1 = "type_half1"; 
	public final static String FIELD_WEATHER_STATUS_HALF1 = "status_half1";
	public final static String FIELD_WEATHER_TYPE_HALF2 = "type_half2"; 
	public final static String FIELD_WEATHER_STATUS_HALF2 = "status_half2";
	public final static String FIELD_WEATHER_TYPE_HALF3 = "type_half3"; 
	public final static String FIELD_WEATHER_STATUS_HALF3 = "status_half3";
	public final static String FIELD_WEATHER_TYPE_HALF4 = "type_half4"; 
	public final static String FIELD_WEATHER_STATUS_HALF4 = "status_half4";
	// 最后更新时间
	// 服务器为最后修改时间
	// 终端为最后一次读服务器时间（用户可通过手工按刷新按钮去链接服务器更新）；所以终端该时间可大于服务器最后修改时间
	public final static String FIELD_WEATHER_UPDATETIME = "updatetime";
	// 版本,从0开始计数
	// 服务器上每做一次修改，版本号加1
	// 对于终端，当版本号与服务器一致时，不更新
	// 该字段也是天气预报 终端与服务器的接口
	// 终端使用该字段去查询服务器端数据，当两边版本号不等时，服务器端返回数据；否则不返回
	public final static String FIELD_WEATHER_VERSION = "version";
	public final static String FIELD_WEATHER_CURRENTTEMP = "ctemprature";
	public final static String FIELD_WEATHER_CSTATUS = "cstatus"; //当前的天气情况
	
	
	/* 球员信息表 */
	public final static String FIELD_PLAYER_ID = "id";
	public final static String FIELD_PLAYER_BAT = "bat";
	public final static String FIELD_PLAYER_BIRTH_ADDRESS = "birth_address";
	public final static String FIELD_PLAYER_BIRTH_BIRTHDAY = "birthday";
	public final static String FIELD_PLAYER_BOUNS  = "bouns";
	public final static String FIELD_PLAYER_CN_NAME = "cn_name";
	public final static String FIELD_PLAYER_CN_SNAME = "cn_sname";
	public final static String FIELD_PLAYER_COUNTRY = "country";
	public final static String FIELD_PLAYER_EN_COUNTRY = "en_country";
	public final static String FIELD_PLAYER_EN_BIRTHADDRESS = "en_birth_address";
	public final static String FIELD_PLAYER_EN_MEMO = "en_memo";
	public final static String FIELD_PLAYER_EN_NAME = "en_name";
	public final static String FIELD_PLAYER_EN_PERMANENTADDRESS = "en_permanentaddress";
	public final static String FIELD_PLAYER_EN_SNAME = "en_sname";
	public final static String FIELD_PLAYER_FACE = "face";
	public final static String FIELD_PLAYER_HEIGHT = "height";
	public final static String FIELD_PLAYER_LAST_RACE_FAIL = "last_race_fail";
	public final static String FIELD_PLAYER_LAST_RACE_WIN = "last_race_win";
	public final static String FIELD_PLAYER_LAST_TURN_COUNT = "last_turn_count";
	public final static String FIELD_PLAYER_LAST_TURN_DATE = "last_turn_Date";
	public final static String FIELD_PLAYER_MEMO = "memo";
	public final static String FIELD_PLAYER_NEWS_RACE_FAIL = "news_race_fail";
	public final static String FIELD_PLAYER_NEWS_RACE_WIN = "news_race_wiin";
	public final static String FIELD_PLAYER_NUM = "num";
	public final static String FIELD_PLAYER_PERMANENT_ADDRESS = "permanent_address";
	public final static String FIELD_PLAYER_PROFESSIONAL_YEAR = "professional_year";
	public final static String FIELD_PLAYER_RACE_CHAM_COUNT = "race_cham_count";
	public final static String FIELD_PLAYER_SEED_NUMBER = "seed_number";
	public final static String FIELD_PLAYER_WEIGHT = "weight";
	public final static String FIELD_PLAYER_GAME_ID = "game_id";
	public final static String FIELD_PLAYER_WILDCARD = "wildcard";
	
	/* 比赛表 */
	public final static String FIELD_MATCH_IS_GROUP = "is_group";
	public final static String FIELD_MATCH_ID = "id";
	public final static String FIELD_MATCH_BEGIN_GIME = "begin_time";
	public final static String FIELD_MATCH_CN_TITLE = "cn_title";
	public final static String FIELD_MATCH_DAY = "day";
	public final static String FIELD_MATCH_MEMO = "memo";
	public final static String FIELD_MATCH_EN_TITLE = "en_title";
	public final static String FIELD_MATCH_IS_SEED = "is_seed";
	public final static String FIELD_MATCH_MATCH_TIME = "match_time";
	public final static String FIELD_MATCH_SECTION_CN_NAME = "section_cn_name";
	public final static String FIELD_MATCH_SECTION_EN_NAME = "section_en_name";
	public final static String FIELD_MATCH_STATICS = "statics";
	public final static String FIELD_MATCH_TURN_ID = "turn_id";
	public final static String FIELD_MATCH_VENUE_ID = "venue_id";
	public final static String FIELD_MATCH_PLAYER_A = "player_a";
	public final static String FIELD_MATCH_PLAYER_RETAIL = "player_retail";
	public final static String FIELD_MATCH_PLAYER_B = "player_b";
	public final static String FIELD_MATCH_WINNER = "winner";
	public final static String FIELD_MATCH_GROUP_A = "group_a";
	public final static String FIELD_MATCH_GROUP_RETAIL = "group_retail";
	public final static String FIELD_MATCH_GROUP_B = "group_b";
	public final static String FIELD_MATCH_GROUP_WINNER = "group_winner";
	public final static String FIELD_MATCH_EN_MATCH_TIME = "en_match_time";
	public final static String FIELD_MATCH_END_TIME = "end_time";
	public final static String FIELD_MATCH_NUM = "match_num";
	
	/* 双打组表 */
	public final static String FIELD_PLAYER_GROUP_ID = "id";
	public final static String FIELD_PLAYER_GROUP_PLAYER_A = "player_a";
	public final static String FIELD_PLAYER_GROUP_PLAYER_B = "player_b";
	public final static String FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER = "group_seed_num_ber";
	public final static String FIELD_PLAYER_GROUP_GROUP_WILDCARD = "group_wildcard";
	
	/* 轮次表 */
	public final static String FIELD_TURN_ID = "id";
	public final static String FIELD_TURN_CN_TITLE = "cn_title";
	public final static String FIELD_TURN_EN_TITLE = "en_title";
	public final static String FIELD_TURN_END_TIME = "end_time";
	public final static String FIELD_TURN_MEMO = "memo";
	public final static String FIELD_TURN_NUM = "turn_num";
	public final static String FIELD_TURN_START_TIME = "start_time";
	public final static String FIELD_TURN_GAME_ID = "game_id";

	/* 数据统计表 */
	public final static String FIELD_DATASTATICSTICE_ID = "id";
	public final static String FIELD_DATASTATICSTICE_ACES_BALL = "aces_ball";
	public final static String FIELD_DATASTATICSTICE_AVG_FIRST = "avg_first";
	public final static String FIELD_DATASTATICSTICE_AVG_SECOND = "avg_second";
	public final static String FIELD_DATASTATICSTICE_BREAK_BALL = "break_ball";
	public final static String FIELD_DATASTATICSTICE_DOUBLE_FAULT = "double_fault";
	public final static String FIELD_DATASTATICSTICE_FASTER = "faster";
	public final static String FIELD_DATASTATICSTICE_FIRST_SUCCESS_BALL = "first_success_ball";
	public final static String FIELD_DATASTATICSTICE_FIRST_WINNER_BALL = "first_winner_ball";
	public final static String FIELD_DATASTATICSTICE_PLAYER_ID = "player_id";
	public final static String FIELD_DATASTATICSTICE_NET_BALL = "net_ball";
	public final static String FIELD_DATASTATICSTICE_RECEIVE_BALL = "receive_ball";
	public final static String FIELD_DATASTATICSTICE_SECOND_WINN_BALL = "second_winn_ball";
	public final static String FIELD_DATASTATICSTICE_TOTAL_BREAK_BALL = "total_break_ball";
	public final static String FIELD_DATASTATICSTICE_TOTAL_FIRST = "total_first";
	public final static String FIELD_DATASTATICSTICE_TOTAL_NET_BALL = "total_net_ball";
	public final static String FIELD_DATASTATICSTICE_TOTAL_POINT = "total_point";
	public final static String FIELD_DATASTATICSTICE_TOTAL_RECEIVE_BALL = "total_receive_ball";
	public final static String FIELD_DATASTATICSTICE_TOTAL_SECOND = "total_second";
	public final static String FIELD_DATASTATICSTICE_UNFORCE = "unforce";
	public final static String FIELD_DATASTATICSTICE_WINN_BALL = "winn_ball";
	public final static String FIELD_DATASTATICSTICE_MATHC_ID = "match_id";
	public final static String FIELD_DATASTATICSTICE_BREAK_RELATED = "break_related";
	public final static String FIELD_DATASTATICSTICE_SERVE_RECEIVE_SCORING_RATE = "serve_receive_scoring_rate";
	public final static String FIELD_DATASTATICSTICE_SITUATION_GOT_SCORING_RATE = "situation_got_scoring_rate";
	public final static String FIELD_DATASTATICSTICE_THE_NET_SCORING_RATE = "the_net_scoring_rate";
	public final static String FIELD_DATASTATICSTICE_TWO_FAT_SCORING_RATE = "two_fat_scoring_rate";
	
	
	/* 赛事表 */
	public final static String FIELD_GMAES_ID = "id";
	public final static String FIELD_GMAES_END_TIME = "end_time";
	public final static String FIELD_GMAES_NAME = "name";
	public final static String FIELD_GMAES_START_TIME = "start_time";
	public final static String FIELD_GMAES_CODE = "code";
	
	
	/* 总分表 */
	public final static String FIELD_TOTAL_ID = "id";
	public final static String FIELD_TOTAL_TOTAL_ONE = "total_one";
	public final static String FIELD_TOTAL_TOTAL_TWO = "total_two";
	public final static String FIELD_TOTAL_MATCH_ID = "match_id";
	
	
	/* 局分表 */
	public final static String FIELD_SENTENCE_SCORE_ID = "id";
	public final static String FIELD_SENTENCE_SCORE_SENTENCE_ONE = "sentence_one";
	public final static String FIELD_SENTENCE_SCORE_SENTENCE_TWO = "sentence_two";
	public final static String FIELD_SENTENCE_SCORE_MATCH_ID = "match_id";
	

	/* 场馆表 */
	public final static String FIELD_VENUES_ID = "id";
	public final static String FIELD_VENUES_CN_NAME = "cn_name";
	public final static String FIELD_VENUES_EN_NAME = "en_name";
	public final static String FIELD_VENUES_MEMO = "memo";
	public final static String FIELD_VENUES_NUM = "venue_num";
	public final static String FIELD_VENUES_GAME_ID = "game_id";
	
	/* 盘分数表 */
	public final static String FIELD_SCORE_ID = "id";
	public final static String FIELD_SCORE_NUM = "num";
	public final static String FIELD_SCORE_SCORE_A = "score_a";
	public final static String FIELD_SCORE_SCORE_B = "score_b";
	public final static String FIELD_SCORE_SMALL_SCORE_A = "small_score_a";
	public final static String FIELD_SCORE_SMALL_SCORE_B = "small_Score_B";
	public final static String FIELD_SCORE_MATCH_ID = "match_id"; 
	
	/*咨询服务中心*/
	public final static String FIELD_CONSULT_ID = "id";
	public final static String FIELD_CONSULT_CONTENT = "content";
	public final static String FIELD_CONSULT_SEND_TIME = "send_time";
	
	/*回复同步表*/
	public final static String FIELD_UPLOAD_REPLY_ID = "upload_reply_id";
	public final static String FIELD_UPLOAD_REPLY_SERVICE_ID = "service_command_id";
	public final static String FIELD_UPLOAD_REPLY_CONTENT = "content";
	public final static String FIELD_UPLOAD_REPLY_SEND_TIME = "send_time";
	
	//最新比分
	public final static String FIELD_LATEST_SCORE_PLAYA= "playera";
	public final static String FIELD_LATEST_SCORE_PLAYB= "playerb";
	public final static String FIELD_LATEST_SCORE_SCOREA= "scorea";
	public final static String FIELD_LATEST_SCORE_SCOREB= "scoreb";
	
	// 最新比分
	private final static String SQL_TB_LATEST_SCORE = "CREATE TABLE "+TB_LATEST_SCORE+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_LATEST_SCORE_PLAYA+" varchar(30),"
	+" "+FIELD_LATEST_SCORE_PLAYB+" varchar(30),"
	+" "+FIELD_LATEST_SCORE_SCOREA+" varchar(30),"
	+" "+FIELD_LATEST_SCORE_SCOREB+" varchar(30));";
	
	public void saveLatestScore(Game g)
	{
		if(g!=null)
		{
			openDB();
			db.delete(TB_LATEST_SCORE, null, null);
			ContentValues values = new ContentValues();
			values.put(FIELD_LATEST_SCORE_PLAYA, g.one);
			values.put(FIELD_LATEST_SCORE_PLAYB, g.other);
			values.put(FIELD_LATEST_SCORE_SCOREA, g.oneScore);
			values.put(FIELD_LATEST_SCORE_SCOREB, g.otherScore);
			db.insert(TB_LATEST_SCORE, FIELD_ID, values);	
		}		
	}
	
	
	// 重要通知
	private final static String SQL_IMPORT_NOTICE = "CREATE TABLE "+TB_IMPORT_NOTICE+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_IMPORT_NOTICE_ID+" integer,"
	+" "+FIELD_IMPORT_NOTICE_APPTYPE+" integer,"
	+" "+FIELD_IMPORT_NOTICE_CONTENT+" text,"
	+" "+FIELD_IMPORT_NOTICE_IMAGE_WEBURL+" varchar(100) ,"
	+" "+FIELD_IMPORT_NOTICE_IMAGE_LOCALURL+" varchar(100) ,"
	+" "+FIELD_IMPORT_NOTICE_ISSUE_TIME+" varchar(30),"
	+" "+FIELD_IMPORT_NOTICE_ISSUER+" varchar(30),"
	+" "+FIELD_IMPORT_NOTICE_SEND_TIME+" varchar(30),"
	+" "+FIELD_IMPORT_NOTICE_SENDER+" varchar(30),"
	+" "+FIELD_IMPORT_NOTICE_SEND_TYPE+" varchar(30),"
	+" "+FIELD_IMPORT_NOTICE_ISREAD+" integer,"
	+" "+FIELD_IMPORT_NOTICE_REPLYCONTENT+" text,"
	+" "+FIELD_IMPORT_NOTICE_TOP+" varchar(5),"
	+" "+FIELD_IMPORT_NOTICE_REPLYTIME+" varchar(30));";
	
	
	
	
	/* 重要通告(已发送，我发送给别人的) */ 
	private final static String SQL_IMPORT_NOTICE_SEND = "CREATE TABLE "+TB_IMPORT_NOTICE_SEND+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_IMPORT_NOTICE_SENT_APPTYPE+" integer,"
	+" "+FIELD_IMPORT_NOTICE_SENT_SERVERID+" varchar(30),"
	+" "+FIELD_IMPORT_NOTICE_CONTENT+" text,"
	+" "+FIELD_IMPORT_NOTICE_SENT_IMAGE_LOCALURL+" varchar(100) ,"
	+" "+FIELD_IMPORT_NOTICE_SENT_SEND_TIME+" varchar(30) ,"	
	+" "+FIELD_IMPORT_NOTICE_SENT_ISSUER+" varchar(30) ,"	
	+" "+FIELD_IMPORT_NOTICE_SENT_SENDER+" varchar(30) ,"
	+" "+FIELD_IMPORT_NOTICE_SENT_SEND_TYPE+" varchar(30) ,"
	+" "+FIELD_IMPORT_NOTICE_SENT_ISSUE_TIME+" varchar(30));";
	
	
	
	
	/* 重要通告(已发送子表，记录每条信息的接受者和回复状态) ；注意：本张表暂时只使用前面两个字段
	 * 查看接受者状态的实现，都去读服务器URL*/	
	 private final static String SQL_IMPORT_NOTICE_SEND_CHILD = "CREATE TABLE "+TB_IMPORT_NOTICE_SEND_CHILD+" ("
	 	+" "+FIELD_ID+" integer primary key autoincrement, "
	 	+" "+FIELD_IMPORT_NOTICE_SENT_CHILD_ID+" varchar(30),"
		+" "+FIELD_IMPORT_NOTICE_SENT_CHILD_APPTYPE+" varchar(30),"
		+" "+FIELD_IMPORT_NOTICE_SENT_RECEIVER+" integer,"
		+" "+FIELD_IMPORT_NOTICE_SENT_REPLYCONTENT+" text,"
		+" "+FIELD_IMPORT_NOTICE_SENT_ISREAD+" integer ,"	
		+" "+FIELD_IMPORT_NOTICE_SENT_REPLYTIME+" varchar(30));";
	
	
 
	private final static String SQL_PERSON = "CREATE TABLE "+TB_PERSON+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_PERSON_ID+" integer,"
	+" "+FIELD_PERSON_NAME+" text,"
	+" "+FIELD_PERSON_DEPARTMENT+" integer,"
	+" "+FIELD_PERSON_MOBILE+" varchar(30) ,"
	+" "+FIELD_PERSON_IMEI+" varchar(30),"
	+" "+FIELD_PERSON_SHORT_MOBILE+" varchar(30),"
	+" "+FIELD_PERSON_LATITUDE+" varchar(30),"
	+" "+FIELD_PERSON_LONGITUDE+" varchar(30) ,"
	+" "+FIELD_PERSON_IS_LEADER+" integer ,"
	+" "+FIELD_PERSON_POSITION+" varchar(30));";
	
	private final static String SQL_PERSON2 = "CREATE TABLE "+TB_PERSON2+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_PERSON2_ID+" integer,"
	+" "+FIELD_PERSON2_NAME+" text,"
	+" "+FIELD_PERSON2_DEPARTMENT+" integer,"
	+" "+FIELD_PERSON2_MOBILE+" varchar(30) ,"
	+" "+FIELD_PERSON2_IMEI+" varchar(30),"
	+" "+FIELD_PERSON2_SHORT_MOBILE+" varchar(30),"
	+" "+FIELD_PERSON2_IS_LEADER+" integer ,"
	+" "+FIELD_PERSON2_POSITION+" varchar(30));";
	
	private final static String SQL_DEPARTMENT = "CREATE TABLE "+TB_DEPARTMENT+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_DEPARTMENT_ID+" integer, "
	+" "+FIELD_DEPARTMENT_NAME+" varchar(30));";
	
	private final static String SQL_POSITION = "CREATE TABLE "+TB_POSITION+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_POSITION_ID+" integer,"
	+" "+FIELD_POSITION_NAME+" varchar(30));";
	
	private final static String SQL_PLAYER = "CREATE TABLE "+TB_PLAYER+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_PLAYER_ID+" varchar(30) default '',"
		+" "+FIELD_PLAYER_BAT+" varchar(1) default '',"
		+" "+FIELD_PLAYER_BIRTH_ADDRESS+" text default '',"
		+" "+FIELD_PLAYER_BIRTH_BIRTHDAY+" varchar(20) default '',"
		+" "+FIELD_PLAYER_BOUNS+" varchar(255) default '',"
		+" "+FIELD_PLAYER_CN_NAME+" varchar(128) default '',"
		+" "+FIELD_PLAYER_CN_SNAME+" varchar(64) default '',"
		+" "+FIELD_PLAYER_COUNTRY+" varchar(128) default '',"
		+" "+FIELD_PLAYER_EN_COUNTRY+" varchar(128) default '',"
		+" "+FIELD_PLAYER_EN_BIRTHADDRESS+" varchar(255) default '',"
		+" "+FIELD_PLAYER_EN_MEMO+" text default '',"
		+" "+FIELD_PLAYER_EN_NAME+" varchar(128) default '',"
		+" "+FIELD_PLAYER_EN_PERMANENTADDRESS+" varchar(255) default '',"
		+" "+FIELD_PLAYER_EN_SNAME+" varchar(64) default '',"
		+" "+FIELD_PLAYER_FACE+" varchar(255) default '',"
		+" "+FIELD_PLAYER_HEIGHT+" varchar(30) default '',"
		+" "+FIELD_PLAYER_LAST_RACE_FAIL+" varchar(30) default '',"
		+" "+FIELD_PLAYER_LAST_RACE_WIN+" varchar(30) default '',"
		+" "+FIELD_PLAYER_LAST_TURN_COUNT+" varchar(30) default '',"
		+" "+FIELD_PLAYER_LAST_TURN_DATE+" varchar(30) default '',"
		+" "+FIELD_PLAYER_MEMO+" text default '',"
		+" "+FIELD_PLAYER_NEWS_RACE_FAIL+" varchar(30) default '',"
		+" "+FIELD_PLAYER_NEWS_RACE_WIN+" varchar(30) default '',"
		+" "+FIELD_PLAYER_NUM+" varchar(30),"
		+" "+FIELD_PLAYER_PERMANENT_ADDRESS+" varchar(128) default '',"
		+" "+FIELD_PLAYER_PROFESSIONAL_YEAR+" varchar(20) default '',"
		+" "+FIELD_PLAYER_RACE_CHAM_COUNT+" varchar(30) default '',"
		+" "+FIELD_PLAYER_SEED_NUMBER+" varchar(30) default '',"
		+" "+FIELD_PLAYER_WEIGHT+" varchar(30) default '',"
		+" "+FIELD_PLAYER_GAME_ID+" varchar(30) default '',"
		+" "+FIELD_PLAYER_WILDCARD+" varchar(1) default '');";
	
	private final static String SQL_MATCH = "CREATE TABLE "+TB_MATCH+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_MATCH_IS_GROUP+" varchar(31) default '',"
		+" "+FIELD_MATCH_ID+" varchar(30) default '',"
		+" "+FIELD_MATCH_BEGIN_GIME+" varchar(20) default '',"
		+" "+FIELD_MATCH_CN_TITLE+" varchar(128) default '',"
		+" "+FIELD_MATCH_DAY+" varchar(10) default '',"
		+" "+FIELD_MATCH_MEMO+" text default '',"
		+" "+FIELD_MATCH_EN_TITLE+" varchar(128) default '',"
		+" "+FIELD_MATCH_IS_SEED+" varchar(1) default '',"
		+" "+FIELD_MATCH_MATCH_TIME+" varchar(20) default '',"
		+" "+FIELD_MATCH_SECTION_CN_NAME+" varchar(128) default '',"
		+" "+FIELD_MATCH_SECTION_EN_NAME+" varchar(128) default '',"
		+" "+FIELD_MATCH_STATICS+" varchar(1) default '',"
		+" "+FIELD_MATCH_TURN_ID+" varchar(30) default '',"
		+" "+FIELD_MATCH_VENUE_ID+" varchar(30) default '',"
		+" "+FIELD_MATCH_PLAYER_A+" varchar(30) default '',"
		+" "+FIELD_MATCH_PLAYER_RETAIL+" varchar(30) default '',"
		+" "+FIELD_MATCH_PLAYER_B+" varchar(30) default '',"
		+" "+FIELD_MATCH_WINNER+" varchar(30) default '',"
		+" "+FIELD_MATCH_GROUP_A+" varchar(30) default '',"
		+" "+FIELD_MATCH_GROUP_RETAIL+" varchar(30) default '',"
		+" "+FIELD_MATCH_GROUP_B+" varchar(30) default '',"
		+" "+FIELD_MATCH_GROUP_WINNER+" varchar(30) default '',"
		+" "+FIELD_MATCH_EN_MATCH_TIME+" varchar(128) default '',"
		+" "+FIELD_MATCH_END_TIME+" varchar(128) default ''," 
		+" "+FIELD_MATCH_NUM+" varchar(30) default '');";
	
	private final static String SQL_PLAYER_GROUP = "CREATE TABLE "+TB_PLAYER_GROUP+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_PLAYER_GROUP_ID+" varchar(30) default '',"
		+" "+FIELD_PLAYER_GROUP_PLAYER_A+" varchar(30) default '',"
		+" "+FIELD_PLAYER_GROUP_PLAYER_B+" varchar(30) default '',"
		+" "+FIELD_PLAYER_GROUP_GROUP_SEED_NUM_BER+" varchar(1) default '',"
		+" "+FIELD_PLAYER_GROUP_GROUP_WILDCARD+" varchar(1) default '');";
	
	private final static String SQL_TURN = "CREATE TABLE "+TB_TURN+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_TURN_ID+" varchar(30),"
		+" "+FIELD_TURN_CN_TITLE+" varchar(128),"
		+" "+FIELD_TURN_EN_TITLE+" varchar(128),"
		+" "+FIELD_TURN_END_TIME+" varchar(20),"
		+" "+FIELD_TURN_MEMO+" text,"
		+" "+FIELD_TURN_NUM+" varchar(30),"
		+" "+FIELD_TURN_START_TIME+" varchar(20),"
		+" "+FIELD_TURN_GAME_ID+" varchar(30));";
	
	private final static String SQL_DATASTATISTICS = "CREATE TABLE "+TB_DATASTATICSTICS+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_DATASTATICSTICE_ID+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_ACES_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_AVG_FIRST+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_AVG_SECOND+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_BREAK_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_DOUBLE_FAULT+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_FASTER+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_FIRST_SUCCESS_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_FIRST_WINNER_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_PLAYER_ID+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_NET_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_RECEIVE_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_SECOND_WINN_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_TOTAL_BREAK_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_TOTAL_FIRST+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_TOTAL_NET_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_TOTAL_POINT+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_TOTAL_RECEIVE_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_TOTAL_SECOND+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_UNFORCE+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_WINN_BALL+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_MATHC_ID+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_BREAK_RELATED+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_SERVE_RECEIVE_SCORING_RATE+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_SITUATION_GOT_SCORING_RATE+" varchar(30),"
		+" "+FIELD_DATASTATICSTICE_THE_NET_SCORING_RATE+" varchar(30),"	
		+" "+FIELD_DATASTATICSTICE_TWO_FAT_SCORING_RATE+" varchar(30));";
	
	private final static String SQL_GAMES = "CREATE TABLE "+TB_GAMES+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_GMAES_ID+" varchar(30),"
		+" "+FIELD_GMAES_END_TIME+" varchar(20),"
		+" "+FIELD_GMAES_NAME+" varchar(64),"
		+" "+FIELD_GMAES_START_TIME+" varchar(20),"
		+" "+FIELD_GMAES_CODE+" varchar(16));";
	
	private final static String SQL_TOTAL = "CREATE TABLE "+TB_TOTAL+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_TOTAL_ID+" varchar(30),"
		+" "+FIELD_TOTAL_TOTAL_ONE+" varchar(30),"
		+" "+FIELD_TOTAL_TOTAL_TWO+" varchar(30),"
		+" "+FIELD_TOTAL_MATCH_ID+" varchar(30));";

	private final static String SQL_SENTENCE_SCORE = "CREATE TABLE "+TB_SENTENCE_SCORE+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_SENTENCE_SCORE_ID+" varchar(30),"
		+" "+FIELD_SENTENCE_SCORE_SENTENCE_ONE+" varchar(30),"
		+" "+FIELD_SENTENCE_SCORE_SENTENCE_TWO+" varchar(30),"
		+" "+FIELD_SENTENCE_SCORE_MATCH_ID+" varchar(30));";
	
	private final static String SQL_VENUE = "CREATE TABLE "+TB_VENUES+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_VENUES_ID+" varchar(30),"
		+" "+FIELD_VENUES_CN_NAME+" varchar(128),"
		+" "+FIELD_VENUES_EN_NAME+" varchar(128),"
		+" "+FIELD_VENUES_MEMO+" varchar(128),"
		+" "+FIELD_VENUES_NUM+" varchar(30),"
		+" "+FIELD_VENUES_GAME_ID+" varchar(30));";

	private final static String SQL_SCORE = "CREATE TABLE "+TB_SCORE+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_SCORE_ID+" varchar(30) default '',"
		+" "+FIELD_SCORE_NUM+" varchar(30) default '',"
		+" "+FIELD_SCORE_SCORE_A+" varchar(30) default '',"
		+" "+FIELD_SCORE_SCORE_B+" varchar(30) default '',"
		+" "+FIELD_SCORE_SMALL_SCORE_A+" varchar(30) default '',"
		+" "+FIELD_SCORE_SMALL_SCORE_B+" varchar(30) default '',"
		+" "+FIELD_SCORE_MATCH_ID+" varchar(30) default '');";
	
	private final static String SQL_WEAHTER = "CREATE TABLE "+TB_WEATHER+" ("+FIELD_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_WEATHER_DATE+" varchar(30) default '',"
	+" "+FIELD_WEATHER_MESSAGE+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD1+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD1+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD2+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD2+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD3+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD3+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD4+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD4+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD5+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD5+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD6+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD6+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD7+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD7+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD8+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD8+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD9+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD9+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD10+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD10+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD11+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD11+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_PERIOD12+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_PERIOD12+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_HALF1+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_HALF1+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_HALF2+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_HALF2+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_HALF3+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_HALF3+" varchar(30) default '',"
	+" "+FIELD_WEATHER_TYPE_HALF4+" varchar(30) default '',"
	+" "+FIELD_WEATHER_STATUS_HALF4+" varchar(30) default '',"
	+" "+FIELD_WEATHER_UPDATETIME+" varchar(30) default '',"
	+" "+FIELD_WEATHER_VERSION+" varchar(30) default '',"
	+" "+FIELD_WEATHER_CSTATUS+" varchar(30) default '',"
	+" "+FIELD_WEATHER_CURRENTTEMP+" varchar(30) default '')";
	
	
	private final static String SQL_CONSULT = "CREATE TABLE "+TB_CONSULT+" ("+FIELD_ID
		+" integer primary key autoincrement, "
		+" "+FIELD_CONSULT_ID+" varchar(30), "
		+" "+FIELD_CONSULT_SEND_TIME+" varchar(20), "
		+" "+FIELD_CONSULT_CONTENT+" text);";
	
	private final static String SQL_UPLOAD_REPLY = "CREATE TABLE IF NOT EXISTS "+TB_UPLOAD_REPLY+" ("+FIELD_UPLOAD_REPLY_ID
	+" integer primary key autoincrement, "
	+" "+FIELD_UPLOAD_REPLY_SERVICE_ID+" varchar(30), "
	+" "+FIELD_UPLOAD_REPLY_CONTENT+" varchar(500), "
	+" "+FIELD_UPLOAD_REPLY_SEND_TIME+" varchar(30));";
		
	private final static String SQL_DEPARTMENT_INSERT = "INSERT INTO " + TB_DEPARTMENT + " VALUES(1,'1','总经理室11');INSERT INTO " + TB_DEPARTMENT + " VALUES(2,'2','党政办公室');";

	 class ContactsTask extends AsyncTask<Void, Void, Void>{
		   @Override
		protected Void doInBackground(Void... params) {
			try{
				Tools.buildContacts(context,queryAll(DBHelper.TB_PERSON));
			}catch(Exception e){
				Log.e("--------ContactsTask","Error");
			}
			return null;
		}
	   }

}
